So I’ve got a script where the character turns towards the movement direction. So for example, if I hold “A” the they will turn and move left on the x axis in world space.
What I need now, is for the character’s rotation to be acting in relation with the camera’s forward direction.
Here’s the code I have:
public Transform playerCamera;
public float moveSpeed;
public float rotationSpeed;
float translation;
float strafe;
void FixedUpdate () {
//Movement
strafe = Input.GetAxis ("Horizontal") * moveSpeed * Time.deltaTime;
translation = Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime;
Vector3 movement = new Vector3 (strafe, 0f, translation);
if (movement != Vector3.zero) {
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement), rotationSpeed);
}
transform.Translate (playerCamera.TransformDirection(movement));
}
And here’s a simpler explanation:
What I have now
The character will always turn to the key input movement in world space, so if I were to rotate the camera 90 degrees to the right, and then press “A”, the player will now move backwards, or towards the camera since that is still “left” in world space.
What I need
The character takes the forward direction of the camera in regard when turning. So if I turn the camera like before (90 degrees to the right), and then press “A”, the character will now move forward in world space, which will be “left” in the camera’s view. Pretty much what happens in MMOs.
And just to clarify, I don’t want the player’s rotation to always be relevant to the camera. So I don’t want the player turning with it, I just want the camera’s forward direction to act as the movement turning basis for the character, rather than the world space.
Any idea how I could change that, or add to it, to make the character’s movement rotation be relevant to the camera’s forward position rather than world space?