Hey there, I’m sure this is a simple one but I cannot wrap my head around it. Basically, I have a 3D 3rd Person game, and these objects:
- Player that can move on X and Z,
- Camera Rig that follows player and can be rotated on Y
- Main Camera attached to Camera Rig
I’ve managed to have the player move using the horizontal and vertical input-axis relative to the camera rotation, but now I want him to rotate accordingly as well. I’m just not sure what values to mix to get it done - ideally, I would want the player to not just jump into the correct rotation, but actually have him gradually rotate there. I appreciate any solutions, ideas and nods into the right direction, pun not intended. Thanks in advance!
Here be the code for the Player:
Rigidbody rb;
float hAxis; //Input Horizontal Axis
float vAxis; //Input Vertical Axis
public GameObject camRig;
Transform camRot; //Y-rotation of camera
public float moveSpeed;
void Start () {
rb = GetComponent<Rigidbody> ();
camRot = camRig.transform;
}
void Update () {
hAxis = Input.GetAxis ("Horizontal");
vAxis = Input.GetAxis ("Vertical");
transform.Translate (
camRot.forward * vAxis * moveSpeed * Time.deltaTime+
camRot.right*hAxis*moveSpeed*Time.deltaTime,
Space.World);
// Rotating when moving
if (hAxis != 0 || vAxis != 0){
//Code for rotating the player when he is in movement goes here
}
}