how can i get the camera to always stay rotated behind the player with this player movement script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float force;
public Rigidbody rb;
public Camera cam;
public Vector3 InputVector;
public Vector3 VelocityAmount;
public float XsmoothedVelocityAmount;
public float ZsmoothedVelocityAmount;
public Vector2 rbVelocityMaxLimits;
public float smoothMovementTime;
public float smoothRotationTime;
void FixedUpdate ( ) {
float horiz = Input.GetAxis ( "Horizontal" );
float verti = Input.GetAxis ( "Vertical" );
// Set input
InputVector = new Vector3 (
horiz,
0.0f,
verti
);
// Set movement speed & its' maximum
// & minimum values
VelocityAmount = new Vector3 (
Mathf.Clamp (
( InputVector.x * force ),
-rbVelocityMaxLimits.x,
rbVelocityMaxLimits.x
),
0.0f,
Mathf.Clamp (
( InputVector.z * force ),
-rbVelocityMaxLimits.y,
rbVelocityMaxLimits.y
)
);
// Flatten camera rotation
Vector3 camForward = cam.transform.forward;
camForward.y = 0.0f;
Quaternion camRotationFlattened = Quaternion.LookRotation ( camForward );
// Make movement relative to camera
VelocityAmount = camRotationFlattened * VelocityAmount;
// Set smooth movement
XsmoothedVelocityAmount = Mathf.SmoothStep (
rb.velocity.x,
VelocityAmount.x,
smoothMovementTime
);
ZsmoothedVelocityAmount = Mathf.SmoothStep (
rb.velocity.z,
VelocityAmount.z,
smoothMovementTime
);
// Set rotation to movement direction
if ( InputVector.x != 0.0f || InputVector.z != 0.0f ) {
transform.rotation = Quaternion.Lerp (
transform.rotation,
Quaternion.LookRotation ( VelocityAmount ),
smoothRotationTime
);
}
// Update current speed
rb.velocity = new Vector3 (
XsmoothedVelocityAmount,
0.0f,
ZsmoothedVelocityAmount
);
}
}
Any help is GREATFULLY appreciated!
Thanks & have a great evening!