How to move/rotate cube forward/back/left/right in C#?

Hi everyone!

I’m trying to make a cube move and rotate (forward, back, right and left) using a C# script.

Still, I have a slight problem which is that I can’t change the axis on which I move my cube any time I want. I can move/rotate it forward and back, no problem. I can move/rotate it right and left, no problem. But if I move it 2 times to the left and then forward for example, the cube doesn’t move the way it should anymore.

Remember that with “transform.Up/forward/back/left/right/down” these are all relative to the object’s orientation.
The reason why your topples fail the second time… is because after the first topple, transform.up isn’t up anymore. The object might now be lying on it’s side, so transform.up might actually be pointing left…

So what does this mean? To make your rotations based on the world’s notion of up, forward, right etc… you should think in WORLD space instead of local space…
so use Vector3.up/down/right etc… instead.
This will give you the world axis in those directions… they won’t change and will always be the same no matter how many times your cube rotates.

You just do this

Note that if yOU DO want to do it with physics (rather than “abstractly” as in a puzzle game), then: Add a Rigidbody to your cube and add torque. (You’ll need a floor under your cube)

    public float torque;
	public Rigidbody rb;
	void Start() {
		rb = GetComponent<Rigidbody>();
	void FixedUpdate() {
		float turn = Input.GetAxis("Horizontal");
		rb.AddTorque(transform.forward * torque * turn);

Tweak the torque variable until you get enough force to topple the cube.

This is a well-known trivial technique in vid game development. It’s a duplicate of 100s of identical questions on here.

full tutorial … Roll cuboid does not work properly - Unity Answers

You just move a “rotation point” around and then flexibly “re-parent” your cube to that.

It’s a basic everyday technique to master in vid games.