Unity collision not being detected

2019-09-14 13:34发布

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

标签: unity3d
3条回答
We Are One
2楼-- · 2019-09-14 14:12

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.

查看更多
Melony?
3楼-- · 2019-09-14 14:13

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".

查看更多
做自己的国王
4楼-- · 2019-09-14 14:21

I recommend to verify collision. Here on simple example:

void OnCollisionEnter (Collision col){
        if (col.gameObject){
            Debug.Log("Object name : "+ col.gameObject.name);
        }
    }
查看更多
登录 后发表回答