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);
}
}