- Home /
Rotating left or right via keyboard input
I've searched around and cant find anything of use.
I'm trying to recreate lunar lander(http://moonlander.seb.ly/) and am tackling the rotation of the ship but with my current script, the ship will constantly rotate to the right and when I do press input, it will rotate it left correctly.
In my input manager I have 'Horizontal' set up to accept the negative button as 'left' and positive as 'right'
Here is my script:
public class RocketController : MonoBehaviour {
private Rigidbody2D rocket;
public int rotationSpeed;
void Start () {
rocket = gameObject.GetComponent<Rigidbody2D>();
}
void Update () {
if (Input.GetButton("Horizontal"))
{
rocket.transform.Rotate(Vector3.forward * rotationSpeed * Time.deltaTime);
}else if (!Input.GetButton("Horizontal"))
{
rocket.transform.Rotate(Vector3.back * rotationSpeed * Time.deltaTime);
}
}
}
Answer by ElementalVenom · Oct 13, 2018 at 03:46 PM
What your doing in the code is this: IF (a left/right button is pressed) THEN (rotate to the right); ELSE IF ( left/right button is NOT pressed) THEN (rotate to the left);
======================
To fix this simply do the following in actual code(so dont copy-paste this because its not actual code): IF(Input.Get("Horizontal") >0.5f) THEN (Rotate right); ELSE IF(Input.Get("Horizontal")<-0.5f) THEN (rotate left);
Your answer
Follow this Question
Related Questions
Have an object continuously rotate towards another 1 Answer
What´s wrong with my rotation function? 0 Answers
Bullet dissapears from screen when I use LookAt? 0 Answers
Player Arm will not rotate when moving with Xbox Joystick 1 Answer
How to return gameobjects rotation to original Rotation / zero rotation? 1 Answer