- Home /
How to lock down the rotation when keys are pressed?
So I have a little spaceship game going where you press the arrow keys and move your ship around, and I want it to subtly rotate slightly when going left and right but then return to upright position afterwards, but with what I have when you hold down the arrow key like you have too to move, it causes it to continually rotate instead of just once and I can't figure out how to limit it.
if (Input.GetKey(KeyCode.LeftArrow))
{
gameObject.transform.Translate(Vector3.left * 3f);
RotateLeft();
}
Comment
Answer by Ki4Chan · Sep 14, 2017 at 02:46 PM
I think this will work, I have not tried it. But if this doesn't work please let me know I'll try it and correct it.
bool isGoingLeft = false;
bool isGoingRight = false;
void MoveRight()
{
if (isGoingRight)
{
//Your code to tilt it to right
}
else if (isGoingLeft)
{
//your code to tilt it left
}
else
{
//upright position
}
}
void GoingRight()
{
if (Input.GetKey("right"))
{
isGoingRight = true;
}
else
{
isGoingLeft = false;
}
}
void GoingLeft()
{
//same as above method with little change.
}
}