-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.cs
More file actions
73 lines (60 loc) · 1.72 KB
/
Copy pathProjectile.cs
File metadata and controls
73 lines (60 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEngine;
public class Projectile : MonoBehaviour
{
[SerializeField] private float speed;
private float direction;
private bool hit;
private float lifetime;
private Animator animator;
private BoxCollider2D boxCollider;
private TilePainter tp;
private void Awake()
{
animator = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
tp = FindObjectOfType<TilePainter>();
if (tp == null)
{
Debug.LogError("TilePainter not found in the scene.");
}
}
private void Update()
{
if (hit) return;
float movementSpeed = speed * Time.deltaTime * direction;
transform.Translate(movementSpeed, 0, 0);
lifetime += Time.deltaTime;
if (lifetime > 5)
{
Deactivate();
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (!hit)
{
hit = true;
boxCollider.enabled = false;
animator.SetTrigger("explode");
}
}
public void SetDirection(float _direction)
{
lifetime = 0;
direction = _direction;
gameObject.SetActive(true);
hit = false;
boxCollider.enabled = true;
float localScaleX = transform.localScale.x;
if (Mathf.Sign(localScaleX) != _direction)
{
localScaleX = -localScaleX;
}
transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
}
// This method is called by an animation event
public void Deactivate()
{
gameObject.SetActive(false);
}
}