Home > Net >  Unity: player walks into trigger and animation plays once
Unity: player walks into trigger and animation plays once

Time:04-12

how do I make it that "Player walks into a trigger and an animation plays once and if the player walks into the same trigger nothing will happen" There are no videos on youtube nor other websites that I know that will explain how to do this. Im also New to unity and am not the best when doing animations

CodePudding user response:

You need to add a trigger for your animation in the player's Animation Controller.

You could disable the trigger once you walk into it by doing:

private void OnTriggerEnter(Collider other)
{
    // Disables trigger so it doesn't trigger again
    other.enabled = false;
    // Triggers animation, "AnimTrigger" should refer to the trigger you set
    // in your animators settings
    Animator anim = GetComponent<Animator>();
    anim.SetTrigger("AnimTrigger");
}

CodePudding user response:

In the script attached to the trigger volume, simply add a boolean triggered that stores whether it has been triggered yet.

// in your script
triggered = false;
private void OnTriggerEnter(Collider other) {
     if (triggered) return;
     // do your stuff here
     trigerred = true;
}
  • Related