W.Oosthuizen
W.Oosthuizen

Reputation: 5

Endless Runner in Unity

I have a project where I have to create an endless runner game in unity. The problem I have is in the collision aspect of the game where the sphere and one of the objects collide I want the sphere to be destroyed. This is the code I have for it:

private void OnCollisionEnter(Collision other)
{
    if (other.gameObject.tag == "lethal")
    {
        Destroy(gameObject);
    }                      
}

I tagged the objects within the game as lethal. The problem is even with this code the sphere, when colliding, is not destroyed rather its just an obstacle that stops the ball rather than destroying it.

Any help? not sure what i am doing wrong

Upvotes: 0

Views: 497

Answers (1)

Jeredriq Demas
Jeredriq Demas

Reputation: 761

  • Disable the physics between them. If your game lags and destruction takes place slow, your "runner" will be disturbed by this
  • Instead of collider use ontriggerentered
  • Do not use string comparisons with "==" instead use equals or CompareTag as @Jichael suggested.

And if you really want to use physics, make a child object to your runner and that should trigger the collision instead of your runner. And give the tag to that child object

Upvotes: 1

Related Questions