1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// This component is attached to a particle system which is not the UI root, because otherwise
- /// the shuriken particle system will freak out due to scaling (it will not render when transform is not visible)
- /// So this makes the particle system follow a target without the need of being under the scaling
- /// of the UI root.
- /// </summary>
- public class SpermicidalWaveFollower : MonoBehaviour
- {
- Transform _target;
- Transform _cachedTransform;
- float _originalZPosition;
- void Awake()
- {
- _cachedTransform = transform;
- _originalZPosition = _cachedTransform.position.z;
- NotificationCenter.AddListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.AddListener(OnGamePause, NotificationType.GamePause);
- NotificationCenter.AddListener(OnGameResume, NotificationType.GameResume);
- }
- void OnDestroy()
- {
- NotificationCenter.RemoveListener(OnGameOver, NotificationType.GameOver);
- NotificationCenter.RemoveListener(OnGamePause, NotificationType.GamePause);
- NotificationCenter.RemoveListener(OnGameResume, NotificationType.GameResume);
- }
- void OnEnable()
- {
- //Just in case it was previously hidden
- ChangeZPosition(_originalZPosition);
- }
- void LateUpdate()
- {
- if (_target != null)
- {
- if (_target.gameObject.activeSelf)
- {
- Vector3 newPos = _target.position;
- newPos.z = _cachedTransform.position.z;
- _cachedTransform.position = newPos;
- } else
- {
- StopFollowing();
- }
- }
- }
- void OnGameOver(Notification note)
- {
- StopFollowing();
- }
- void OnGamePause(Notification note)
- {
- //This is to hide it, so it doesn't conflict with the pause menu.
- //This is a hack due to mixing 2D with 3D
- ChangeZPosition(5);
- }
- void OnGameResume(Notification note)
- {
- ChangeZPosition(_originalZPosition);
- }
- public void StartFollowing(Transform target)
- {
- _target = target;
- var m = GetComponent<ParticleSystem>().emission;
- m.enabled = true;
- }
- void StopFollowing()
- {
- _target = null;
- var m = GetComponent<ParticleSystem>().emission;
- m.enabled = false;
- }
- void ChangeZPosition(float newZ)
- {
- Vector3 newPos = _cachedTransform.position;
- newPos.z = newZ;
- _cachedTransform.position = newPos;
- }
- }
|