-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectile.cs
37 lines (33 loc) · 1.24 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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Projectile : MonoBehaviour
{
public int damage = 5;
public Vector2 moveSpeed = new Vector2(10f,0);
public Vector2 knockback = new Vector2(0, 0);
Rigidbody2D rb;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb.linearVelocity = new Vector2(moveSpeed.x * transform.localScale.x, moveSpeed.y);
}
private void OnTriggerEnter2D(Collider2D collision)
{
Damageable damageable = collision.GetComponent<Damageable>();
if(damageable != null)
{
// If Parent is facing the left by localScale, our knockback x flips its value to face the left as well
Vector2 deliveredKnockback = transform.localScale.x > 0 ? knockback : new Vector2(-knockback.x, knockback.y);
// hit the target
bool gotHurt = damageable.hurt(damage, deliveredKnockback);
if (gotHurt)
Debug.Log(collision.name + "hurt for " + damage);
Destroy(gameObject);
}
}
}