Home>

I want it to explode when it hits an enemy bullet, but it will explode as soon as it begins to explode with my bulletUnity Code // Spaceship component
Spaceship spaceship;

IEnumerator Start ()
{
// Get the Spaceship component
spaceship = GetComponent();

while (true)
{

// Create a bullet at the same position/angle as the player
spaceship.Shot (transform);

// Wait for shotDelay seconds
yield return new WaitForSeconds (spaceship.shotDelay);
}
}

void Update ()
{
// Right/Left
float x = Input.GetAxisRaw ("Horizontal");

// Up/Down
float y = Input.GetAxisRaw ("Vertical");

// Find the direction to move
Vector2 direction = new Vector2 (x, y) .normalized;

// Move
spaceship.Move (direction);
}

// Call if bumped
void OnTriggerEnter2D (Collider2D c)
{
// Delete the bullet
Destroy (c.gameObject);

// Explode
spaceship.Explosion ();

// Remove player
Destroy (gameObject);

  • Answer # 1

    I think you should branch using the tag of the target object inOnTriggerEnter2D

    Gog by function name and tag.

    (There are also ways to control hits using layers, so check it out)