This is my code:
public class MagicBehaviour : MonoBehaviour
{
Rigidbody2D magicRb2d;
PlayerMovement player;
[SerializeField] float bulletSpeed = 5f;
float xSpeed;
private void Start()
{
magicRb2d = GetComponent<Rigidbody2D>();
player = FindObjectOfType<PlayerMovement>();
xSpeed = player.transform.localScale.x * bulletSpeed;
}
void Update()
{
magicRb2d.velocity = new Vector2(10f * xSpeed, 0f);
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
Destroy(other.gameObject);
}
Destroy(gameObject);
}
}
I didn’t have to implement the second destruction for the ‘bullet’ to destroy. I found that it just destroyed when it collided with anything else. I’m not sure why, but I assume it is because of the other.gameObject.tag
?
I remembered that Rick used other.gameObject.tag
in the delivery driver section, so I used it instead and it worked perfectly for me