- Home /
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 :)
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);
}
}
I'm confused about your movement model. Typically movement code assigns "vertical" to move the object forward and backwards and "horizontal" to rotate the object. Sometimes the model is one of a compass where horizontal and vertical is combined to indicate a direction of movement. Your code doesn't fit into either model cleanly. Plus it is unusual to use Transform.Translate() for movement but to use Rigidbody.$$anonymous$$oveRotation() for the rotation. Please provide a description of how "Horizontal" and "Vertical" should be applied to the rotation and movement of the object.
@robertbu, you're right, I'm sorry I will update the question in the hope to make myself more clear.
Answer by kevinv321 · Jul 12, 2013 at 01:36 PM
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);
Answer by amphoterik · Jul 12, 2013 at 11:56 AM
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.
@amphoterik Thanks a lot for your answer! You code indeed works I started thinking, its the same as $$anonymous$$e, so after a bit playing with my code I found out that the problem is the rotation script. That makes my cube confused, any chance you could give a solution without losing the rotation effect?
Your answer
![](https://koobas.hobune.stream/wayback/20220613105935im_/https://answers.unity.com/themes/thub/images/avi.jpg)