简体   繁体   中英

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

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM