我正朝着玩家的方向走A.I角色。一旦它的collider2D通过使用Add force进入玩家并且工作正常且花花公子,但是当A.I退出玩家collider2D时它继续增加力量。所以我添加onTriggerExit2D()来强制0或停止应用它。
由于某种原因,触发器永远不会退出,我已经使用过调试但没有任何东西可以显示。
void Update () {
if (canHearPlayer)
{
rigidbody2D.AddForce (new Vector2 (-force, 0));
}
if (!canHearPlayer)
{
force = 0;
}
}
void FixedUpdate()
{
}
void OnTriggerEnter2D(Collider2D other){
Debug.Log(gameObject.name + " was hit by " + other.gameObject.name);
if (other.gameObject.name == "New_Player")
{
canHearPlayer = true;
force = 3f;
}
}
void onTriggerExit2D(Collider2D other)
{
if (other.gameObject.name == "New_Player")
{
canHearPlayer = false;
Debug.LogWarning (canHearPlayer + "Trigger has been exited");
}
}
答案 0 :(得分:4)
该功能不起作用,因为字母" o"是小写而不是大写。
更改此行:
void onTriggerExit2D(Collider2D other)
为:
void OnTriggerExit2D(Collider2D other)