简体   繁体   中英

onTriggerExit2D() not working? unity2D c#

I am making an AI character walk towards the player. Once it's collider2D enters the players by using Add force and that works fine and dandy, but when the AI exits the players collider2D it keeps adding the force. So I added onTriggerExit2D() to either make the force 0 or to stop applying it.

For some reason the trigger is never exited I have used debugging but nothing ever shows.

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");
    }
}

The function isn't working because the letter "o" is lowercase instead of uppercase.

Change this line:

void onTriggerExit2D(Collider2D other)

to:

void OnTriggerExit2D(Collider2D other)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM