fbpx

 

Third Person Camera...
 

Third Person Camera Issue  

   RSS

0

I have created a script (mostly copied someone else's) in order to create a third-person camera that follows the player with a little delay and allows the mouse to scroll around the player. The issue is that currently the player doesn't rotate to run in the direction the camera is facing, it just runs forward in the same direction (when you press up).

I added a line of code (commented to highlight it below) which will turn the player in the direction of the camera, but the camera turns with it causing it to spin. I am pretty sure I know why. Because the target is a child of the player it rotates with the player, the rotation of the camera is based on the rotation of the target though, so this causes the camera to rotate with the player.

Is there a simple way of fixing this? My camera follow code is shown below:

public class SmoothFollow : MonoBehaviour {

public Transform target, player;

private float _distance = 3;
private float _maxDriftRange = .5f;
private float _rotationSpeed = 2;
private float _pitch, _yaw;

private Transform m_transform_cache;
private Transform myTransform {
get {
if (m_transform_cache == null) {
m_transform_cache = transform;
}
return m_transform_cache;
}
}

void Start() {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}

void FixedUpdate() {
Vector3 targetPos = GetTargetPos();
float t = Vector3.Distance(myTransform.position, targetPos) / _maxDriftRange;

_pitch -= Input.GetAxis("Mouse Y") * _rotationSpeed;
_pitch = Mathf.Clamp(_pitch, -35, 60);
_yaw += Input.GetAxis("Mouse X") * _rotationSpeed;

myTransform.position = Vector3.Lerp(myTransform.position, targetPos, t * Time.deltaTime);
myTransform.LookAt(target);

// this is meant to rotate the player in the direction of the camera when you press up or down.
if (Input.GetAxisRaw("Vertical") == 1) {
player.rotation = new Quaternion(player.rotation.x, transform.rotation.y, player.rotation.z, transform.rotation.w);
}
}

private Vector3 GetTargetPos() {
Vector3 targetPos = new Vector3(0, 0, -_distance);
targetPos = Quaternion.Euler(_pitch, _yaw, 0) * targetPos;
return target.position + (target.rotation * targetPos);
}
}
1 Answer
1

Good news all. I actually managed to fix it myself.

Basically this section:

  private Vector3 GetTargetPos() {
Vector3 targetPos = new Vector3(0, 0, -_distance);
targetPos = Quaternion.Euler(_pitch, _yaw, 0) * targetPos;
return target.position + (target.rotation * targetPos);
}

Needed to be changed to this:

  private Vector3 GetTargetPos() {
Vector3 targetPos = new Vector3(0, 0, -_distance);
targetPos = Quaternion.Euler(_pitch, _yaw, 0) * targetPos;
return target.position + targetPos;
}

Everything is now functioning just like I want. 🙂

I did Lerp the rotation too so the player doesn't instantly face a different direction.

If anyone has suggestions as to improvements though, I'd love to hear them.

Share:

 

 

Scroll to Top

Please Login or Register