Lawrence William Trigg - 28 days ago 6x

C# Question

so I am brand new to Unity, Installed Unity yesterday and want to make a simple game to get myself started...

So I am trying to get a 3D cube to turn by rotation while the up and down moves the cube.

I found out kind of what i was doing following a 2D tutorial online but it seems like it never worked in 3D as my cube instead of rotating around, either tries to tilt over or not move (changing the z y and x on the rotation)

Here is my code, any help would be appreciated or directions to a good tutorial for what i am after would be brilliant!

`public class Movement : MonoBehaviour {`

float maxSpeed = 4f;

float rotSpeed = 180f;

void Start () {

}

void Update () {

//ROTATE SHIP

Quaternion rot = transform.rotation;

float x = rot.eulerAngles.x;

x += Input.GetAxis ("Horizontal") * rotSpeed * Time.deltaTime;

rot = Quaternion.Euler (0, 0, x);

transform.rotation = rot;

//MOVE SHIP

Vector3 pos = transform.position;

pos.z += Input.GetAxis ("Vertical") * maxSpeed * Time.deltaTime;

transform.position = pos;

}

}

Answer

change this line:

```
rot = Quaternion.Euler (0, 0, x);
```

to this line:

```
rot = Quaternion.Euler (x, 0, 0);
```

You were taking original x and assigning it back as z;

Source (Stackoverflow)

Comments