PlayerWaterProjectile.cs 1.64 KB
Newer Older
1
2
3
4
5
6
using UnityEngine;

namespace _Game.Scripts.Player.PlayerAttacks.Water
{
	public class PlayerWaterProjectile : MonoBehaviour
	{
Sebastian Frey's avatar
Sebastian Frey committed
7
		public float destroyTime = 2.0f;
Sebastian Frey's avatar
Sebastian Frey committed
8
		public float pushFactor = 1.0f;
9
10
		private float _startTime;
		private float _deltaTime;
11
12
		private float _newScaleFactor;
		private Vector3 _newScaleVector;
13
		private Vector3 _startingScale;
14
15
		private Rigidbody2D _rigidbody2D;
		private Vector2 _startingVelocity;
16
17
18
19
20

		private void Start()
		{
			_startTime = Time.time;
			_startingScale = this.gameObject.transform.localScale;
21
			_rigidbody2D = GetComponent<Rigidbody2D>();
22
23
24
25
		}

		private void Update()
		{
26
			_deltaTime = (Time.time - _startTime) / destroyTime;
27
28
29
30
31
32
33
34
			if (_deltaTime >= 0.8f)
			{
				_newScaleFactor = (1 - _deltaTime) * 5.0f;
			}
			else
			{
				_newScaleFactor = 1.0f;
			}
35

36
			_newScaleVector = new Vector3(_newScaleFactor * _startingScale.x, _newScaleFactor * _startingScale.y, 0.0f);
37

38
			this.gameObject.transform.localScale = _newScaleVector;
39
		}
40

Sebastian Frey's avatar
Sebastian Frey committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		public void OnCollisionEnter2D(Collision2D other)
		{
			if (other.gameObject.layer != LayerMask.NameToLayer("Ground"))
			{
				if (other.gameObject.layer == LayerMask.NameToLayer("MovableObject"))
				{
					if (Mathf.Abs(_rigidbody2D.velocity.x) > 0.0001f)
					{
						var projectileVelocityX = _rigidbody2D.velocity.x / Mathf.Abs(_rigidbody2D.velocity.x);
						print(projectileVelocityX);
						other.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector3(projectileVelocityX * pushFactor, 0.0f, 0.0f);
					}

				}
				Destroy();
			}
		}

59
60
		private void OnEnable()
		{
61
			Invoke(nameof(Destroy), destroyTime);
62
63
64
65
66
67
68
69
		}

		private void Destroy()
		{
			Destroy(gameObject);
		}
	}
}