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!
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);
}
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);
}
}
‘Function’ isn’t working, how do I fix this?