You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

65 lines
1.7 KiB

using UnityEngine;
using System.Collections;
[RequireComponent (typeof(Rigidbody))]
[RequireComponent (typeof(Collider))]
public class Projectile : MonoBehaviour {
public float speed = 10;
public DIRECTION direction;
public bool destroyOnHit;
public GameObject EffectOnSpawn;
private DamageObject damage;
public bool firedByEnemy = false;
void Start () {
GetComponent<Rigidbody>().velocity = new Vector2((int)direction * speed, 0);
GetComponent<Collider>().isTrigger = true;
//turn projectile to travel direction
transform.rotation = Quaternion.Euler(0f, (direction == DIRECTION.Right? 180 : 0), 0f);
//show an effect when this projectile is spawned
if(EffectOnSpawn) {
GameObject effect = GameObject.Instantiate(EffectOnSpawn) as GameObject;
effect.transform.position = transform.position;
}
}
//tell the player that an item is in range
void OnTriggerEnter(Collider coll) {
if (firedByEnemy)
{
if (coll.CompareTag("Player"))
{
//hit a damagable object
IDamagable<DamageObject> damagableObject = coll.GetComponent(typeof(IDamagable<DamageObject>)) as IDamagable<DamageObject>;
if (damagableObject != null)
{
damagableObject.Hit(damage);
if (destroyOnHit) Destroy(gameObject);
}
}
}
else
{
if (coll.CompareTag("Enemy"))
{
//hit a damagable object
IDamagable<DamageObject> damagableObject = coll.GetComponent(typeof(IDamagable<DamageObject>)) as IDamagable<DamageObject>;
if (damagableObject != null)
{
damagableObject.Hit(damage);
if (destroyOnHit) Destroy(gameObject);
}
}
}
}
//sets the damage of this projectile
public void SetDamage(DamageObject d){
damage = d;
}
}