- Home /
2 animations in same button (door open-close)
Hi everyone I'm making this door script but I'm blocked this scipt will do door open-close animations I can open it but I can't close it please help me out because I'm annoyed of this
public GameObject text;
public GameObject dot;
public Animator oynatici;
public bool acildimi = false; //door status checker
// Use this for initialization
void Start () {
text = GameObject.Find("Oyun_canvas/bildirim_text").gameObject;
dot = GameObject.Find("Oyun_canvas/Dot").gameObject;
oynatici = GameObject.Find("kapi_cerceve/on_kapi_cevirici").gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
// I don't want use here for performance issues
}
public void check()
{
if (Input.GetKeyDown(KeyCode.E))
{
acildimi = true;
oynatici.Play("kapi_ac");
}
else if ((Input.GetKeyDown(KeyCode.E)) && (acildimi==true))
{
acildimi = false;
oynatici.Play("kapi_kapa");
}
}
public void kelime()
{
dot.SetActive(false);
if (acildimi==true)
{
text.gameObject.GetComponent<Text>().text = "Kapıyı Kapat";
}
else
{
text.gameObject.GetComponent<Text>().text = "Kapıyı aç";
}
}
private void OnTriggerEnter(Collider other)
{
check();
kelime();
}
private void OnTriggerStay(Collider other)
{
check();
kelime();
}
private void OnTriggerExit(Collider other)
{
text.gameObject.GetComponent<Text>().text = "";
dot.SetActive(true);
}
}
Answer by madks13 · Jul 23, 2018 at 10:19 AM
The triggers, add a Debug.Log and check how often they trigger.
Edit : @wolfgangmami nevermind, i found the error :
In your check, you check the button press, and in the else if you check it again, that means you will always enter the first case, since the animation trigger is button press. Add "&& !acildimi" in the first check to differentiate between door states.
Why am I adding it. If triggers not working I can't open door
You're missing the point. I never said they didn't work, i said you should check how often they trigger.
I still didn't get you so I added it debug.log showed up one time(first enter)showed up like crazy (when I'm stay)
Answer by Ellie97 · Jul 24, 2018 at 09:19 AM
Have you thought about creating an animator?
https://docs.unity3d.com/Manual/Animator.html
https://unity3d.com/learn/tutorials/topics/animation/animator-controller
Edit: if you're worried about performance, it's also not wise to use GameObject.Find. You're better off using Tags or public variables assigned in the inspector
wolfgangmami, check my updated answer, i think i found the problem.