I think I messed up the syntax or something. The code only works if it is the last thing in the update function. If I write code below it, it seems to not run it. Unity says the error is on line 33 where I check if the tag associated with the collision has the tag "player".
public class EnemysProjectile : MonoBehaviour
{
public float speed;
public float lifetime;
public float distance;
public LayerMask whatIsSolid;
public float damage;
public LayerMask whatIsPlayer;
void Start()
{
Invoke("DestroyProjectile", lifetime);
}
void Update()
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, transform.up, distance, whatIsSolid);
RaycastHit2D ifHitPlayer = Physics2D.Raycast(transform.position, transform.up, distance, whatIsPlayer);
if (hitInfo.collider != null && hitInfo.collider.CompareTag("Enemy") == false)
{
DestroyProjectile();
}
if (ifHitPlayer.collider.CompareTag("Player") == true)
{
ifHitPlayer.collider.GetComponent().DamageToPlayer(damage);
DestroyProjectile();
}
}
void DestroyProjectile()
{
Destroy(gameObject);
}
}
↧