- Home /
Collisions not working.
I am working on a small game that uses a "game over" screen that is displayed upon a collision with a pit (a cube with black material.) The cube itself has a rigidbody and box collider.
This is the script (Javascript) attached to my player (First Person Controller Camera, has a RigidBody and Capsule Collider with no trigger.)
function OnCollisionEnter(deadPlayer : Collision)
{
if (deadPlayer.gameObject.tag == "Pit")
{
Destroy (gameObject);
Application.LoadLevel("deathPit");
}
}
The player properly collides with the cube, rather, it acts as a floor. But the level specified in the last line is not loaded, and I stay in the bottom of the pit until I exit the game.
Any help would be appreciated. :c
does the player get destroyed? are you sure the "if" statement is correctly executed?
The player doesn't get destroyed. It just stays on the cube as if it's a floor; nothing happens.
Did you forget to tag your player object inside the inspector? Try to tag your object with $$anonymous$$ on the inspector window while your player object is selected.
The player is tagged as "Player" and the pit is tagged as "$$anonymous$$".
Debug.Log
is your friend. Log messages right after you enter OnCollisionEnter
, log the value of the tag, and so on. Do you get the first log message? If not then the collision isn't being detected for some reason - probably some colliders not configured correctly. What does the tag display as? The comparison is case sensitive, so make sure it's "$$anonymous$$", not "pit", etc...
function OnCollisionEnter(deadPlayer : Collision)
{
Debug.Log("Collision");
Debug.Log("tag=" + deadPlayer.gameObject.tag));
if (deadPlayer.gameObject.tag == "$$anonymous$$")
{
Debug.Log("Found pit");
...
}
else
{
Debug.Log("Didn't find pit");
}
Answer by Tatsunomi · Jul 24, 2013 at 06:35 PM
Try Placing this script on your Pit Object... See if this works
function OnCollisionEnter(deadPlayer : Collision)
{
if (deadPlayer.gameObject.tag == "Player")
{
Debug.Log("HIT");
Destroy(GameObject.FindGameObjectWithTag("Player"));
Application.LoadLevel("death");
}
}
Depending on where you placed the script I decided to place this in the GameObject tagged Pit so when the other object tagged "Player" hits "pit". The "Player" gameobject will be destroyed and the level will be loaded.