proto_top_down/Assets/Scripts/Projectile.cs

42 lines
954 B
C#
Raw Normal View History

using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Projectile : MonoBehaviour
{
private Transform _transform;
private Rigidbody2D _rigidbody;
private float _startTime;
public float speed = 7;
public float lifeTime = 5;
public float damage = 2;
private void Start()
{
_transform = transform;
_rigidbody = GetComponent<Rigidbody2D>();
_rigidbody.gravityScale = 0;
_startTime = Time.time;
}
private void Update()
{
_transform.position += _transform.up * (speed * Time.deltaTime);
if (_startTime + lifeTime < Time.time)
{
Destroy(gameObject);
}
}
// TODO: ignore owner
private void OnCollisionEnter2D(Collision2D collision)
{
var health = collision.gameObject.GetComponent<Health>();
if (health != null)
{
health.DealDamage(damage);
}
}
}