-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.cs
More file actions
102 lines (84 loc) · 3.06 KB
/
Projectile.cs
File metadata and controls
102 lines (84 loc) · 3.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using UnityEngine;
public class Projectile : MonoBehaviour
{
[HideInInspector] public Vector3 direction;
[SerializeField] private float speed = 1f;
private BossLevelManager bossLevelManager;
[HideInInspector] public Transform playerTransform;
[SerializeField] private float lifetime = 5f;
[SerializeField] private float playerDeathDelay = 1f;
[HideInInspector] public static Vector3 targetPosAfterReverse;
[SerializeField] private float damageToBoss = 1f;
private bool isReturning = false;
private void Start()
{
bossLevelManager = FindObjectOfType<BossLevelManager>();
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
targetPosAfterReverse = transform.position;
if(playerTransform == null)
{
return;
}
if (gameObject.tag == "PowerfulArrow")
{
transform.rotation = Quaternion.Euler(0f, 0f, 180f);
}
else
{
transform.rotation = Quaternion.Euler(0f, 0f, -135f);
}
SetDirection();
Destroy(gameObject, lifetime);
}
private void Update()
{
// Move the projectile in its forward direction
if (playerTransform == null)
return;
if(isReturning)
{
transform.position += direction * speed * playerTransform.gameObject.GetComponent<BossKillerPlayerScript>().hitForce * Time.deltaTime;
}
else
{
transform.position += direction * speed * Time.deltaTime;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
// Deal damage to player
Destroy(other.gameObject, playerDeathDelay);
UIManager.Instance.PopUpAnim(bossLevelManager.losePanel, Vector3.one);
VFXManager.Instance.PlayEffect(VFXManager.Instance.bloodEffect, other.transform.position);
SoundManager.PlaySound(SoundType.HIT);
SoundManager.PlaySound(SoundType.LOSE);
Destroy(gameObject, 0.1f);
}
else if(other.CompareTag("Bat"))
{
if (gameObject.tag == "PowerfulArrow")
return;
direction = (targetPosAfterReverse - other.transform.position).normalized;
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 45f); // Rotate an arrow
Debug.Log("Player hit the ball");
isReturning = true;
}
if(other.CompareTag("Boss"))
{
if(isReturning)
{
other.GetComponent<Boss>().TakeDamage(damageToBoss);
Debug.Log("Boss hit");
Destroy(gameObject);
isReturning = false;
}
}
}
protected virtual void SetDirection()
{
direction = (playerTransform.position - transform.position).normalized;
}
}