- Home /
Rotating an Object on Key Press!
Hello, I am new to JavaScript but I would like to know how to rotate an object on its X axis using the A-key (rotate left) and the D-key (rotate right).
Can anyone please input a working JavaScript code that will allow an object to rotate using these keys?
Much appreciated!
Answer by robertbu · Jan 26, 2013 at 04:48 AM
Here is a quick script. Attach it to any object. Note it rotates around Y axis (since you mentioned left and right). If you really want the X axis, replace Vector3.up with Vector3.right.
var speed = 30;
function Update () {
if (Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.up * speed * Time.deltaTime);
if (Input.GetKey(KeyCode.D))
transform.Rotate(-Vector3.up * speed * Time.deltaTime);
}
This is what I have been looking for! Great idea, thanks so much!
Thanks! Great info for a noob such as me. :) Now off to get my F-16 to fly!
Answer by Matheus_Henrique-3D · Feb 01, 2015 at 09:31 AM
correct script:
#pragma strict
var velocidade = 30;
function Start () {
}
function Update () {
if (Input.GetKey(KeyCode.A)){
transform.Rotate(Vector3.up * velocidade * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D)){
transform.Rotate(-Vector3.up * velocidade * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613091612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Key press hides object and spawns another 2 Answers
Rotate on Key press help? 1 Answer
How to get raycast and key click info. 1 Answer
How does unity/javascript handle object references? 1 Answer