- Home /
GetAxisRaw() Ignoring Second Item
What I have is a list of gameobjects that I have attached to the area. The 3 gameobjects are then able to be "cycled" through by the up and down arrows. The issue I'm having is that the key strokes appear to be too sensitive and the input is skipping over the middle item. I can toggle back and forth between item 1 and item 3 but item 2 only gets selected if I get a magic tap on it. See snippet.
void Update () {
if(Input.GetAxisRaw("Vertical") > 0.9)
{
OnSelected(false, Manager[currentMenuItem],speed);
currentMenuItem--;
if(currentMenuItem <0)
{
currentMenuItem = 0;
}
OnSelected(true, Manager[currentMenuItem],speed);
Yield();
}
else if (Input.GetAxisRaw("Vertical") < -0.9)
{
OnSelected(false, Manager[currentMenuItem],speed);
currentMenuItem++;
if(currentMenuItem >= Manager.Length)
{
currentMenuItem = Manager.Length-1;
}
OnSelected(true, Manager[currentMenuItem],speed);
Yield();
}
}</pre>
So once I saw this problem, I setup a Yield function to delay it a certain amount of time. In this case, .25f seconds to prevent it skipping over but it does not appear to be working. See the following:
IEnumerator Yield() { yield return new WaitForSeconds(timedelay); }
What am I doing wrong here to not get a delay?
Answer by Kleptomaniac · Apr 10, 2012 at 05:50 AM
Could I try to simplify this for you? It may be that the GetAxisRaw is updating too fast and currentMenuItem is increasing and decreasing too fast. Perhaps try it like this instead?
void Update ()
{
if (Input.GetButtonDown("Vertical")) {
OnSelected(false, Manager[currentMenuItem],speed);
currentMenuItem += (Input.GetAxisRaw("Vertical")) * -1;
currentMenuItem = Mathf.Clamp(currentMenuItem, 0, (Manager.Length - 1));
OnSelected(true, Manager[currentMenuItem],speed);
}
}
Not sure if that will work. :P
Hope that helps, Klep
That solved it. I definitely could have condensed the code. Glad this works for allow a delay. Everything seems to be running smooth.
Sweet! I'm starting to get sorta good with C# now, just by answering questions! I love UA! Glad I could help mate! :)
Your answer
Follow this Question
Related Questions
Why Is yield return new WaitForSeconds() not working 2 Answers
Definition of Script.function() depends on Script.function()... 3 Answers
About removing the backlight of a button after a set of time 2 Answers
Script gets stuck on WaitForSeconds() 1 Answer
yield WaitForSeconds waits for too long. 2 Answers