1
votes

I'm learning to use Unity3d and I want to rotate a cube diagonally so that it appears to be spinning diagonally when I press the left arrow key + q or right arrow key + w. Can anyone assist me with my query?

This is what I have at the moment:

void Update () 
{
   if(Input.GetKey(KeyCode.UpArrow)) //move forward
      transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

   if(Input.GetKey(KeyCode.DownArrow)) //move backward
      transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);

   if(Input.GetKey(KeyCode.LeftArrow)) //turn left
      transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime);

   if(Input.GetKey(KeyCode.RightArrow)) //turn right
      transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
}
1
Do you mean diagonally as in on 2 different axis? - Tom 'Blue' Piddock
do you mean that the transform.Rotate's that you do, aren't working correctly? Or something else? - Steven Mills
If I press the left arrow key and the q key together, I want the cube to spin toward the top left hand corner. I've tried the rotate function: transform.Rotate(rotateAmount ===>this is a public vec3 variable * Time.deltaTime), but this doesn't seem to work when pressing both keys at the same time. - user1283674

1 Answers

1
votes

try something like this:

if(Input.GetKey(KeyCode.Q)) { //turn diagonally to the left
transform.Rotate(new Vector3 (moveSpeed * Time.deltaTime, 0f, moveSpeed * Time.deltaTime));
}
if(Input.GetKey(KeyCode.E)) { //turn diagonally to the right
transform.Rotate(new Vector3 (moveSpeed * Time.deltaTime, 0f, -moveSpeed * Time.deltaTime));
}

if it's rotating to the wrong direction, replace -moveSpeed with moveSpeed and moveSpeed with -moveSpeed, I hope this works (untested)