我的GameObject开始在地板上休息。 isKinematic设置为true,并且在盒子对撞机上将触发器设置为true。
当玩家触碰对撞机时。它将GameObject向下移动到楼层。然后我将isKinematic设置为false,将触发器设置为false。这会强制游戏对象掉落,直到它撞到地板并停止。我的问题是在游戏对象击中并停止后。我无法让GameObject识别出它与地板相撞。我在OnCollisionEnter2D和OnCollisionStay2D上有一个Debug.Log语句。 Debug.Log在触摸时不会出现在控制台中。这是为什么?
GameObject有一个Rigidbody和Box对撞机。地板上还有一个盒子对撞机和Rigidbody。
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("Player is touching the section");
//sectionRigidbody.isKinematic = true;
if (readyToDrop == false)
//moves player
transform.position += newPosition;
readyToDrop = true;
sectionRigidbody.isKinematic = false;
sectionBoxCollider.isTrigger = false;
sectionRigidbody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
//sectionRigidbody.isKinematic = true;
}
}
void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Floor")
{
Debug.Log ("section is touching the floor");
}
}
void OnCollisionStay2D(Collider2D col)
{
if (col.gameObject.tag == "Floor")
{
Debug.Log ("section is touching the floor");
}
}
答案 0 :(得分:0)
Pay attention when using the collision callback functions with parameter.
OnTriggerEnter2D
takes Collider2D
as parameter. You got this right.
OnCollisionEnter2D
and OnCollisionStay2D
takes Collision2D
as parameter NOT Collider2D
. This is where you failed.
Collider2D
and Collision2D
sounds so simlar but are not the-same.
They won't be called if you got their parameter wrong.
In the latest version of Unity, error will be thrown in the Editor when you make this mistake. It looks something like this:
Script error: OnCollisionEnter2D This message parameter has to be of type: The message will be ignored.
and
Script error: OnCollisionStay2D This message parameter has to be of type: The message will be ignored.
Solution:
Replace
void OnCollisionEnter2D(Collider2D col)
and void OnCollisionStay2D(Collider2D col)
with
void OnCollisionEnter2D(Collision2D col)
and void OnCollisionStay2D(Collision2D col)
.
答案 1 :(得分:0)
您提到触发器设置为true。
触发器不注册与传入的刚体的碰撞。相反,当刚体进入或退出触发体积时,它将发送OnTriggerEnter,OnTriggerExit和OnTriggerStay消息。
来源:https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html