Help with movement cube

Hello all, I have some movement problems with a simple cube object.

The camera position is above the terrain, with a rotation of x = 55 and faces the Z axis. This camera will always be faced this direction so the rotation is always the same, however the position can vary (when the cube moves).

When the user presses arrow up: the cube should go farther from the camera, so + movement on the z axis, when arrow down is pressed: the cube should come closer to the camera so -z movement. When left is pressed, go to left i believe this is -x movement, and right, go to right which will be +x movement.

I am using a rigidbody for rotation, because I saw this used in combination with animation movement in a tutorial, later i will replace this transform translate movement for animation movement :slight_smile:

Status at the moment: when I press left or right, the cube moves towards the camera -z movement and when i press up or down the cube moves from the camera +z axis movement.

UPDATE: it’s the rotating script that causes the strange movement, anyone ideas about how to fix this? I want to cube to look to the direction it is moving.

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
	public float turnSmoothing = 15f;
	public float movementSpeed = 25f;
	
	void FixedUpdate() {
		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis("Vertical");
		MovementManagement(h, v);
	}
	
	void MovementManagement(float horizontal, float vertical) {
		if (horizontal != 0f || vertical != 0f) {
		  Rotating(horizontal, vertical);
		  Move(horizontal, vertical);
		}
	}
	
	void Move(float horizontal, float vertical) {
		Vector3 v3 = new Vector3(horizontal, 0f, vertical);
		transform.Translate(v3, transform);
	}
	
	void Rotating(float horizontal, float vertical) {
		Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
		Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
		Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
		rigidbody.MoveRotation(newRotation);
	}
	
}

You are making this much more complicated than it needs to be. I am assuming that you do not want the camera to move (because you never said anything about it) and that you do not need the cube to rotate. Thus your fixed update method should just be:

void FixedUpdate() {
    float h = Input.GetAxis("Horizontal");
    float v = Input.GetAxis("Vertical");
    transform.Translate(h * speed * Time.deltaTime, 0f, v * speed * Time.deltaTime);
}

I have added Time.deltaTime to make the game consistent on all platforms. I have added a speed variable to easily allow you to control how fast the cube is going. Simply change the speed variable to change the cubes speed.

The movement with translate should be based on world instead of cube since we are rotating the cube with our code. The solution:

Vector3 v3 = new Vector3(horizontal * movementSpeed * Time.deltaTime, 0f, vertical * movementSpeed * Time.deltaTime);
transform.Translate(v3, Space.World);