I think I made this type of thread before, but I don’t remember, and I don’t see anything on my threads list. If I did, I probably didn’t get it solved.
Anyway, I’m trying to make my character’s movement relative to the camera, which I almost fully accomplished. The turning works, and so does the movement on x and z axis. The problem is the y.
Before, the character was turning down when I looked down, and on his back when I looked up. I stopped this by setting the Vector3’s y to 0.
movement.y = 0;
That stops the y rotation in relation to the camera, but not the movement. The movement still tries to move the player’s y position in relation to the camera.
I’ve looked, but I only found an Answers post which had no replies, and a thread which has an a working solution. The problem is, it doesn’t work for me. I copied the code that apparently solved the problem for the OP, but it doesn’t do anything for me. I assumed that in theory it should also work for me, but it seems to be a specific solution to the code the OP had.
Here is code I currently have:
//Movement variables
public float moveSpeed;
float translation;
float strafe;
//Rotation variables
public float rotationSpeed;
public Transform t_Reference;
public Transform t_Camera;
void FixedUpdate () {
//Movement
strafe = Input.GetAxis ("Horizontal") * Time.fixedDeltaTime;
translation = Input.GetAxis ("Vertical") * Time.fixedDeltaTime;
Vector3 movement = new Vector3 (strafe, 0, translation);
movement = Camera.main.transform.TransformDirection(movement);
movement.y = 0;
transform.Translate (movement * moveSpeed, Space.World);
//Rotation
if (movement != Vector3.zero) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement), rotationSpeed);
}
}
void Update () {
t_Reference.eulerAngles = new Vector3(0, t_Camera.eulerAngles.y, 0);
}
Any ideas how to keep the y position relevant to world space rather than the camera’s looking direction?