Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ThirdPersonCamera : MonoBehaviour
- {
- public Transform lookAt,
- camTransform;
- public int invertYControll = -1;
- private Camera cam;
- public float distance = 20.0f,
- currentX = 0.0f,
- currentY = 0.0f,
- height = 6,
- sensitivityX = 4.0f,
- sensitivityY = 1.0f,
- Floor = 0f; // level the player is standing on, helps with camera clipping
- private void Start()
- {
- camTransform = transform;
- cam = Camera.main;
- }
- private void Update()
- {
- //fetch the mouse current position
- currentX += Input.GetAxis("Mouse X");
- currentY += (Input.GetAxis("Mouse Y")) * invertYControll; //used to invert y change to 1 for a non inverted controll.
- currentY = Mathf.Clamp(currentY,-85, 85);
- }
- private void LateUpdate()
- {
- Vector3 dir = new Vector3(0, 0, -distance);
- Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
- Vector3 OrbitPosition = (lookAt.position + new Vector3(0, height, 0)) + rotation * dir;
- //playerposition(+height so at head height) + a rotation based on the mouse* the offset.
- if (OrbitPosition.y < Floor + 0.3f) // if camera is too near the floor, stop it from clipping.
- {
- OrbitPosition.y = Floor + 0.3f;
- }
- camTransform.position = OrbitPosition;//manages where the camera is (orbit)
- camTransform.LookAt(lookAt.position + new Vector3(0,height,0)); //manages where the camera looks (playertarget)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement