Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class ThirdPersonCamera : MonoBehaviour {
  6. //define some constants
  7. private const float LOW_LIMIT = 0.0f;
  8. private const float HIGH_LIMIT = 85.0f;
  9.  
  10. //these will be available in the editor
  11. public GameObject theCamera;
  12. public float followDistance = 5.0f;
  13. public float mouseSensitivityX = 4.0f;
  14. public float mouseSensitivityY = 2.0f;
  15. public float heightOffset = 0.5f;
  16.  
  17. //private variables are hidden in editor
  18. private bool isPaused = false;
  19.  
  20. // Use this for initialization
  21. void Start () {
  22. //place the camera and set the forward vector to match player
  23. theCamera.transform.forward = gameObject.transform.forward;
  24. //hide the cursor and lock the cursor to center
  25. Cursor.visible = false;
  26. Cursor.lockState = CursorLockMode.Locked;
  27. }
  28.  
  29. // Update is called once per frame
  30. void Update () {
  31. //if escape key (default) is pressed, pause the game (feel free to change this)
  32. if (Input.GetButton("Cancel"))
  33. {
  34. //flip the isPaused state, hide/unhide the cursor, flip the lock state
  35. isPaused = !isPaused;
  36. Cursor.visible = !Cursor.visible;
  37. Cursor.lockState = Cursor.lockState == CursorLockMode.Locked ?
  38. CursorLockMode.None : CursorLockMode.Locked;
  39. System.Threading.Thread.Sleep(200);
  40. }
  41.  
  42. if(!isPaused)
  43. {
  44. //if we are not paused, get the mouse movement and adjust the camera
  45. //position and rotation to reflect this movement around player
  46. Vector2 cameraMovement = new Vector2(Input.GetAxis("Mouse X"),Input.GetAxis("Mouse Y"));
  47.  
  48. //first we place the camera at the position of the player + height offset
  49. theCamera.transform.position = gameObject.transform.position + new Vector3(0,heightOffset,0);
  50.  
  51. //next we adjust the rotation based on the captured mouse movement
  52. //we clamp the pitch (X angle) of the camera to avoid flipping
  53. //we also adjust the values to account for mouse sensitivity settings
  54. theCamera.transform.eulerAngles = new Vector3(
  55. Mathf.Clamp(theCamera.transform.eulerAngles.x + cameraMovement.y * mouseSensitivityY, LOW_LIMIT, HIGH_LIMIT),
  56. theCamera.transform.eulerAngles.y + cameraMovement.x * mouseSensitivityX, 0);
  57.  
  58. //then we move out to the desired follow distance
  59. theCamera.transform.position -= theCamera.transform.forward * followDistance;
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement