Note that you should never use GetComponent
in Update
better do it once and reuse the reference.
//Here you store the Animator reference
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
I'ld also use a switch
to define what should happen for which button in order to avoid having the same code over and over again.
private void SetLastPressed(KeyCode code)
{
int value = 0;
switch (code)
{
case KeyCode.None:
value = 0;
break;
case KeyCode.LeftArrow:
value = 1;
break;
case KeyCode.RightArrow:
value = 2;
break;
}
animator.SetInteger("pallotila", value);
lastPressed = code;
}
Simply store and check which button pressed last and make the Input checks exclusive using if-else
.
// Here you store the last pressed key
private KeyCode lastPressed = KeyCode.None;
private void Update()
{
if (lastPressed != KeyCode.LeftArrow && Input.GetKeyDown(KeyCode.LeftArrow))
{
SetLastPressed(KeyCode.LeftArrow);
}
else if (lastPressed != KeyCode.RightArrow && Input.GetKeyDown(KeyCode.RightArrow))
{
SetLastPressed(KeyCode.RightArrow);
}
// If none of the keys is pressed reset
else if (lastPressed != KeyCode.None && !Input.GetKey(KeyCode.LeftArrow) && !Input.GetKey(KeyCode.RightArrow))
{
SetLastPressed(KeyCode.None);
}
// And if only one of them is released but the other one still pressed
//go on using that still pressed key again
else if (lastPressed != KeyCode.LeftArrow && Input.GetKeyUp(KeyCode.RightArrow) &&
Input.GetKey(KeyCode.LeftArrow))
{
SetLastPressed(KeyCode.LeftArrow);
}
else if (lastPressed != KeyCode.RightArrow && Input.GetKeyUp(KeyCode.LeftArrow) &&
Input.GetKey(KeyCode.RightArrow))
{
SetLastPressed(KeyCode.RightArrow);
}
For the movement you could simply reuse the lastPresses
value than as well
if(lastPressed == KeyCode.LeftArrow)
{
transform.position += Vector3.left * nopeus * Time.deltaTime;
transform.rotation = vasemmalle;
}
else if (lastPressed == KeyCode.RightArrow)
{
transform.position += Vector3.right * nopeus * Time.deltaTime;
transform.rotation = oikealle;
}
Additionally you could/should use a List<KeyCode>
to store the last presses. Everytime a button goes down add the button to the end of the list; everytime a button goes up remove it from the list
result => the last pressed button is always the last one in the list.
This makes the return to a previous button way more simple and flexible without adding/changing so much lines of code like
private List<KeyCode> lastPresses = new List<KeyCode>();
private KeyCode lastPressed = KeyCode.None;
private Animator animator;
private void Awake()
{
animator = GetComponent<Animator>();
}
private void SetLastPressed(KeyCode code)
{
int value = 0;
switch (code)
{
case KeyCode.None:
value = 0;
break;
case KeyCode.LeftArrow:
value = 1;
break;
case KeyCode.RightArrow:
value = 2;
break;
}
animator.SetInteger("pallotila", value);
lastPressed = code;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if (!lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Add(KeyCode.LeftArrow);
}
else if (Input.GetKeyUp(KeyCode.LeftArrow))
{
if (lastPresses.Contains(KeyCode.LeftArrow)) lastPresses.Remove(KeyCode.LeftArrow);
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if (!lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Add(KeyCode.RightArrow);
}
else if (Input.GetKeyUp(KeyCode.RightArrow))
{
if (lastPresses.Contains(KeyCode.RightArrow)) lastPresses.Remove(KeyCode.RightArrow);
}
var currentCode = lastPresses.Count > 0 ? lastPresses[lastPresses.Count - 1] : KeyCode.None;
if (currentCode != lastPressed) SetLastPressed(currentCode);
if (lastPressed == KeyCode.LeftArrow)
{
transform.position += Vector3.left * nopeus * Time.deltaTime;
transform.rotation = vasemmalle;
}
else if (lastPressed == KeyCode.RightArrow)
{
transform.position += Vector3.right * nopeus * Time.deltaTime;
transform.rotation = oikealle;
}
}