- Home /
Update GetKey If Statements both playing instantly
I am trying to turn on and off an indicator, I've tried to set it up so that if the left indicator is on and they turn on the right one the left turns off and the right turns on. This part works fine but then I am trying to get it so that if they press the right key (E) when the right indicator is already on it will turn off. Its all inside an update function and my problem is that the on and off parts are both playing instantly and the indicator is either turning off and back on again or on and back off again immediately. How can I prevent this? I have already put in a float that will only equal 1 when indicator is on but the instant it equals one the off if statement triggers. I'm very very confused thanks in advance for any help!
using UnityEngine;
using System.Collections;
public class Indicator : MonoBehaviour
{
public float indicator;
public Renderer indicatorLeft;
public Renderer indicatorRight;
void Start()
{
indicator = 0;
}
void Update()
{
if (Input.GetKeyUp(KeyCode.Q))
{
indicator = 1;
//This part turns on the left indicator.
indicatorLeft.enabled = true;
GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = false;
print("Indicating Left");
//This part turns off the right indicator.
indicatorRight.enabled = false;
GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = true;
print("Right Indicator Off");
}
if (Input.GetKeyUp(KeyCode.E))
{
indicator = 2;
//This part turns on the right indicator.
indicatorRight.enabled = true;
GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = false;
print("Indicating Left");
//This part turns off the left indicator.
indicatorLeft.enabled = false;
GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = true;
print("Indicating Right");
}
if (indicator == 2 && Input.GetKeyDown(KeyCode.E))
{
indicator = 0;
//This part turns off the right indicator.
indicatorRight.enabled = false;
GameObject.FindWithTag("IndicatorRight").GetComponent<AudioSource>().mute = true;
print("Right Indicator Off");
}
if (indicator == 1 && Input.GetKeyDown(KeyCode.Q))
{
indicator = 0;
//This part turns off the left indicator.
indicatorLeft.enabled = false;
GameObject.FindWithTag("IndicatorLeft").GetComponent<AudioSource>().mute = true;
print("Left Indicator Off");
}
}
}
Answer by saschandroid · Jul 18, 2016 at 09:30 AM
private void Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
if (!indicatorLeft.enabled)
{
indicatorLeft.enabled = true;
indicatorRight.enabled = false;
}
else
{
indicatorLeft.enabled = false;
}
}
if ( Input.GetKeyDown(KeyCode.E))
{
if (!indicatorRight.enabled)
{
indicatorLeft.enabled = false;
indicatorRight.enabled = true;
}
else
{
indicatorRight.enabled = false;
}
}
}
Is it this what you want?
Your answer
Follow this Question
Related Questions
Input.GetKey question 1 Answer
Can/how do I set up a boolean to continue an animation after button press 1 Answer
Dialogue with GetKey - Codes 1 Answer
How to get holding key to prioritize 1 Answer
Change view if key is pressed 4 Answers