- Home /
How do i rotate player (aeroplane) using A/D keys while moving Left/Right at the same time
public class CharacterControls : MonoBehaviour
{
public float moveSpeed;
public GameObject Player;
//tranfrom, neutral
public float Neutral = 0f;
public float Transform;
public float zRotation;
public float minClamp = -20f;
public float maxClamp = 20f;
// Start is called before the first frame update
void Start()
{
moveSpeed = 50f;
}
// Update is called once per frame
void Update()
{
transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f);
zRotation = Mathf.Clamp(zRotation - Input.GetAxis("Horizontal") * moveSpeed, minClamp, maxClamp);
transform.eulerAngles = new Vector3(0.0f, zRotation);
}
}
Answer by javiolvecal · May 21, 2020 at 07:09 PM
The problem in your script is that you wrote Input.GetAxis horizontal.
Horizontal axis is A D and Left and Right arrows, so if the movement must be only with the arrows, you must write
void Update()
{
transform.Translate(moveSpeed * Input.GetKey(KeyCode.LeftArrow) * Time.deltaTime, 0f, 0f);
zRotation = Mathf.Clamp(zRotation - Input.GetKey(KeyCode.RightArrow) * moveSpeed, minClamp, maxClamp);
transform.eulerAngles = new Vector3(0.0f, zRotation);
}
Then, to rotate, use the transform.Rotate function. It's the same code that moves the aeroplane, but it will rotate it, but now you must change the Left and Right Arrows to A and D
I hope this helps you :)
Your answer
Follow this Question
Related Questions
Object's vectors are not moving with the object, what did i do wrong? 1 Answer
Need Help Limiting Rotation on Object's X axis 1 Answer
Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag() 2 Answers
How to prevent transform.Rotate from oddly flipping other axis... 1 Answer
,How to clamp my turret rotation? 1 Answer