Reputation: 31
I am learning to create a simple fps game in unity the problem is that the collision does not update itself for example initially when my player is on the ground console prints "floor" by "Debug.log(collision.gameObject)" but when it intersects other objects such as a cube console will print out "cube" but when I walk away from it , console does not change back to "floor" Why????
I am using transform.translate to move and jump and using method OnCollisionEnter for collision detection
Upvotes: 0
Views: 632
Reputation: 183
I recommend to verify collision. Here on simple example:
void OnCollisionEnter (Collision col){
if (col.gameObject){
Debug.Log("Object name : "+ col.gameObject.name);
}
}
Upvotes: 0
Reputation: 131
Remember one thing, the other object you want to collide with need to have a collider component asigned, make sure of it. Join this with the previous answer.
Upvotes: 1
Reputation: 1731
OnCollisionEnter
is triggered only when object enters the collider.
A) Make a list of all encountered objects by adding them when OnCollisionEnter
happens and removing when OnCollisionExit
happens. Then whenever you need to make sure you are on "floor" check it in the list.
B) Use OnCollisionStay
and every frame you will be notified if you are touching the "floor".
Upvotes: 2