Question by
jarthurn · Sep 19, 2018 at 01:40 AM ·
scripting problemtriggerontriggerenteranimationsloop animaiton
Help with non stop looping animation!!
I have this script to open a door once the character gets close to it. In this animation the door opens, stays open for around 5 seconds, and closes after that. But the bool will never set back to false even if im not close to the door anymore, any ways to fix that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorOpeningScript : MonoBehaviour {
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
anim.SetBool("open", true);
}
}
}
Comment
Answer by Inder8288 · Sep 28, 2018 at 12:22 PM
You have to set bool to false manually, You can check if player left the door open area, if so, then use anim.SetBool("open",false) or another way is use "trigger" instead of bool . Here' s the code may work:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorOpeningScript : MonoBehaviour {
private Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
anim.SetBool("open", true);
}
}
private void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
anim.SetBool("open", false);
}
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter triggers many times per trigger. 0 Answers
How do I get OnTriggerEnter to work? 1 Answer
OnTriggerEnter2D being called 25 times. 0 Answers
OnTriggerEvent doesn't work 1 Answer
OnTrigerEnter not working 2 Answers