Question by
sentientdrone · Oct 03, 2016 at 06:01 AM ·
cameracamera-movementthanks
Help with third person camera
Hi, I'm using the following code for a third person camera that orbits around the player with the mouse.
However when the player is upside down or on its side (walking on the roof or walls) it stops functioning as expected. I'm assuming that I need to figure out the orientation of the player and actually rotate a different axis in the quaternions based on that. Any help or hints in the right direction would be greatly appreciated.
cam = transform;
float playerX = player.rotation.eulerAngles.x;
float playerY = player.rotation.eulerAngles.y;
float playerZ = player.rotation.eulerAngles.z;
angleH += Mathf.Clamp(Input.GetAxis("Mouse X"), -1, 1) * horizontalAimingSpeed * Time.deltaTime;
angleV += Mathf.Clamp(Input.GetAxis("Mouse Y"), -1, 1) * verticalAimingSpeed * Time.deltaTime;
angleV = Mathf.Clamp(angleV, minVerticalAngle, maxVerticalAngle);
Quaternion aimRotation = Quaternion.Euler(-angleV, angleH, playerZ);
Quaternion camYRotation = Quaternion.Euler(0, angleH, 0);
cam.rotation = aimRotation;
smoothPivotOffset = Vector3.Lerp(smoothPivotOffset, targetPivotOffset, smooth* Time.deltaTime);
smoothCamOffset = Vector3.Lerp(smoothCamOffset, targetCamOffset, smooth* Time.deltaTime);
cam.position = player.position + (camYRotation * smoothPivotOffset) + (aimRotation * smoothCamOffset);
Comment