-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectile.cs
48 lines (43 loc) · 1.54 KB
/
projectile.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class projectile : MonoBehaviour, IAttacking
{
float lifetime = 2f, curentTime = 0, _knockBackForce = 1500, _damage=5;
public CircleCollider2D col;
private void Start()
{
curentTime = Time.time;
}
// Update is called once per frame
void Update()
{
if(Time.time >= curentTime + lifetime)
{
Destroy(gameObject);
}
Collider2D[] hit = new Collider2D[5];
//contactfilter
ContactFilter2D rigidbody_filter = new ContactFilter2D();
rigidbody_filter.useTriggers = false;
//check whos colliding with projectile
if (col.OverlapCollider(rigidbody_filter, hit) >= 1)
{
if (hit[0].GetComponent<Player>() != null)
{
Player target = hit[0].GetComponent<Player>();
IAttackable player = target.GetComponent<IAttackable>();
Vector2 cur_pos = transform.position, target_pos = target.transform.position;
Vector2 dir = ((target_pos - cur_pos)).normalized;
Vector2 knockback_force = dir * _knockBackForce;
attack(player, knockback_force, _damage);
}
Destroy(gameObject);
}
}
public void attack(IAttackable attacked_object, Vector2 knockback_force, float damage)
{
attacked_object.take_damage(damage, knockback_force);
//Debug.Log("hit");
}
}