-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cs
51 lines (39 loc) · 1.35 KB
/
Bullet.cs
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
using System;
using System.Collections;
using UnityEngine;
public sealed class Bullet : MonoBehaviour
{
[SerializeField, Range(0, 150)] private float maxTravelDistance;
[SerializeField, Range(0, 10)] private int damage = 1;
private Rigidbody2D _rigidbody;
private string _currentTargetToHit;
private Vector2 _shootTarget;
private void Awake() => _rigidbody = GetComponent<Rigidbody2D>();
private void Update()
{
// if(HasReachedMaxDistance()) Destroy(gameObject);
}
public void ActiveBullet(Vector2 shootTarget, float shootingPower, string targetToHit)
{
_currentTargetToHit = targetToHit;
_shootTarget = shootTarget;
_rigidbody.velocity = _shootTarget * shootingPower;
StartCoroutine(DeleteBullet());
}
private IEnumerator DeleteBullet()
{
yield return new WaitForSeconds(10);
Destroy(gameObject);
}
private bool HasReachedMaxDistance()
{
var distance = (Vector2)transform.position - _shootTarget;
return MathF.Abs(distance.magnitude) >= maxTravelDistance;
}
private void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.HasTag("wall")) Destroy(gameObject);
if(!col.gameObject.HasTag(_currentTargetToHit)) return;
col.GetComponent<HealthData>().TakeDamage(damage);
}
}