Advertisement
Syriph

Untitled

Aug 21st, 2019
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. public class ThirdPersonCamera : MonoBehaviour
  2. {
  3. public Transform lookAt,
  4. camTransform;
  5.  
  6. public int invertYControll = -1;
  7.  
  8. private Camera cam;
  9.  
  10. public float distance = 20.0f,
  11. currentX = 0.0f,
  12. currentY = 0.0f,
  13. height = 6,
  14. sensitivityX = 4.0f,
  15. sensitivityY = 1.0f,
  16. Floor = 0f; // level the player is standing on, helps with camera clipping
  17.  
  18. private void Start()
  19. {
  20. camTransform = transform;
  21. cam = Camera.main;
  22. }
  23.  
  24. private void Update()
  25. {
  26. //fetch the mouse current position
  27. currentX += Input.GetAxis("Mouse X");
  28. currentY += (Input.GetAxis("Mouse Y")) * invertYControll; //used to invert y change to 1 for a non inverted controll.
  29.  
  30. currentY = Mathf.Clamp(currentY,-85, 85);
  31. }
  32.  
  33. private void LateUpdate()
  34. {
  35. Vector3 dir = new Vector3(0, 0, -distance);
  36. Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
  37. Vector3 OrbitPosition = (lookAt.position + new Vector3(0, height, 0)) + rotation * dir;
  38. //playerposition(+height so at head height) + a rotation based on the mouse* the offset.
  39. if (OrbitPosition.y < Floor + 0.3f) // if camera is too near the floor, stop it from clipping.
  40. {
  41. OrbitPosition.y = Floor + 0.3f;
  42. }
  43.  
  44. camTransform.position = OrbitPosition;//manages where the camera is (orbit)
  45.  
  46. camTransform.LookAt(lookAt.position + new Vector3(0,height,0)); //manages where the camera looks (playertarget)
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement