public class Projectile : MonoBehaviour
{
public float speed;
public float lifeTime;
public float distance;
public LayerMask whatIsSolid;
public int damage;
private void Start() => Invoke("DestroyProjectile", lifeTime);
private void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.right, distance, whatIsSolid);
if (hitInfo.collider.CompareTag("Enemy"))
{
hitInfo.collider.GetComponent().takeDamage(damage);
}
DestroyProjectile();
}
void DestroyProjectile ()
{
Destroy(gameObject);
}
}
Thats the code, and I get the error every-time I shoot.
↧