- Home /
Using Mecanim Trigger with same Key
Hi,
I'm trying to get a very simple Door opening/closing with Mecanim. I've done this other ways before but I wanted to try get using Mecanim more.
What I'm trying to do seems so simple to me but I really can't figure it out at all, I've looked through previous answers and googled several different tutorials but they all seem to do the same thing, which is open a door with a key press, then the door closes one they exit the trigger area.
However, what I want to do it open the door with "E" for example. Then when the mecanim triggers to open the door, and the door is now "open", the next time I press "E" it will close it.
I have 3 animations; Idle, Open, Closed. I have 2 triggers in Mecanim; Open, Close.
So what I want to do is the first time I press "E", i set the Open trigger. Then the 2nd time I press E its sets the Close trigger..
Here's the code if that helps people see exactly what I mean:
public class OpenDoor : MonoBehaviour { Animator anim; bool inRange = false;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (inRange)
{
if (Input.GetKeyDown(KeyCode.E))
{
anim.SetTrigger("Open");
}
// This is where I'm having trouble..
// I have another anim.SetTrigger("Close") that I also want to
// use with the E input, but I can't figure out how to use it..
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
inRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
inRange = false;
}
}
}
Answer by seckincengiz · Sep 30, 2016 at 06:38 PM
Make a simple switch to check door's current state and set parameter according to it.
void Update()
{
if (inRange)
{
if (Input.GetKeyDown(KeyCode.E) && door)
{
anim.SetTrigger("Open");
door = !door;
}
if (Input.GetKeyDown(KeyCode.E) && !door)
{
anim.SetTrigger("Close");
door = !door;
}
}
}
Hi, thanks for the quick response! So in what you've written would !door not always be the case? As in, both events set door = !door at the end?
Also since this is a beginner question, where/what would i put the door variable as? A simple bool, or do you mean I need to actually make a switch statement?
Sorry for such basic questions, I've been away from Unity for the last few months and it's surprising how much I've forgotten...
Ah ok! I've added a bool for the door, however using what you suggested made it flip between the open/close animation instantly, so the door opened and then closed again with only 1 key press. I'm not sure why it was doing this but i tried encapsulating the if(Input**) into their own if statements so it checked it the bool was true first, then entered the next if statement and it's working exactly how I wanted now. Thanks a lot man for pointing me the right way! :)
Here's the code I ended up muddling together if anyone is interested (feel free to point any bad habits you see in there as I'm still very much a beginner at coding).
using UnityEngine; using System.Collections;
public class OpenDoor : $$anonymous$$onoBehaviour { Animator anim; bool inRange = false; bool door;
void Start()
{
anim = GetComponent<Animator>();
door = true;
}
void Update()
{
if (inRange)
{
if (door)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
anim.SetTrigger("Open");
door = false;
}
}
else if (!door)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
anim.SetTrigger("Close");
door = true;
}
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
inRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
inRange = false;
}
}
}
I'm happy that you solved your problem. And sorry for incomplete answer. Hope it didn't take your time too much.
The reason it was doing that is that whenever the first if
block was entered, it changed the value of door so that the second if
block was entered as well. Simply adding an else
before the second if
would have prevented this (which is basically what you've done).
In reference to your other comment, door = !door
toggles the value of the variable, so it doesn't always set it to the same thing as you were suggesting.
ah I see. Thanks for clarifying Bonfire-Boy!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Access Mecanim/Humanoid Muscles 0 Answers
How would I make a ListChangedEvent? 1 Answer