Question by
Rob64j · Mar 22, 2019 at 12:12 PM ·
2d-platformerontriggerenterontriggerexit
Pressure Switch Issues
Hi all, I'm hoping someone can point me in the right direction with this issue I'm having. In a nutshell; I have a basic 2D platformer containing a pressure switch which animates down with OnTriggerEnter and animates back up with OnTriggerExit - all fine to this point, however if the player returns to the switch OnTriggerEnter/Exit are being called continuously one after another causing the switch to bounce up and down... I can't figure out where I'm going wrong with this and I don't necessarily want the solution straight up (I would rather learn properly) but would love some pointers if anyone has any advice?
Thanks in advance and please see code below;
Animator animator;
Animator playerAnim;
private bool onSwitch = false;
private bool playerTall = false;
private void Start()
{
animator = GetComponent<Animator>();
playerAnim = FindObjectOfType<Player>().GetComponent<Animator>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (playerTall == false && other.tag == ("Player") && onSwitch == false)
{
//Debug.Log("GoTall");
onSwitch = true;
playerAnim.SetTrigger("GoTall");
animator.SetTrigger("Down");
playerTall = true;
}
else if (playerTall == true && other.tag == ("Player") && onSwitch == false)
{
//Debug.Log("GoSquare");
onSwitch = true;
playerAnim.SetTrigger("GoSquare");
animator.SetTrigger("Down");
playerTall = false;
}
}
void OnTriggerExit2D(Collider2D other)
{
//Debug.Log("ExitCollision");
animator.SetTrigger("Up");
onSwitch = false;
}
}
Comment