2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.2 KiB

5 years ago
  1. using UnityEngine;
  2. // Daniel Flanigan, 2014
  3. // This is a combined mouse look and camera move script.
  4. // The cam move script is by: Francis R. Griffiths-Keam
  5. public class MouseLook : MonoBehaviour
  6. {
  7. Vector2 _mouseAbsolute;
  8. Vector2 _smoothMouse;
  9. [Space (20)]
  10. [Header ("Mouse Look Settings :")]
  11. public Vector2
  12. clampInDegrees = new Vector2 (360, 180);
  13. //public bool lockCursor;
  14. public CursorLockMode lockCursor;
  15. public Vector2 sensitivity = new Vector2 (2, 2);
  16. public Vector2 smoothing = new Vector2 (3, 3);
  17. public Vector2 targetDirection;
  18. public Vector2 targetCharacterDirection;
  19. // Assign this if there's a parent object controlling motion, such as a Character Controller.
  20. // Yaw rotation will affect this object instead of the camera if set.
  21. public GameObject characterBody;
  22. [Space (20)]
  23. [Header ("Camera Move Settings :")]
  24. public float acceleration = 1.0f;
  25. public float maxSpeed = 5;
  26. public float dampingSpeed = 0.2f;
  27. public KeyCode fwdKey = KeyCode.W;
  28. public KeyCode leftKey = KeyCode.A;
  29. public KeyCode backKey = KeyCode.S;
  30. public KeyCode rightKey = KeyCode.D;
  31. private float speedX, speedZ=0;
  32. void Start ()
  33. {
  34. // Set target direction to the camera's initial orientation.
  35. targetDirection = transform.localRotation.eulerAngles;
  36. // Set target direction for the character body to its inital state.
  37. if (characterBody)
  38. targetCharacterDirection = characterBody.transform.localRotation.eulerAngles;
  39. }
  40. void Update ()
  41. {
  42. // Ensure the cursor is always locked when set
  43. //Screen.lockCursor = lockCursor;
  44. Cursor.lockState = lockCursor;
  45. // Allow the script to clamp based on a desired target value.
  46. var targetOrientation = Quaternion.Euler (targetDirection);
  47. var targetCharacterOrientation = Quaternion.Euler (targetCharacterDirection);
  48. // Get raw mouse input for a cleaner reading on more sensitive mice.
  49. var mouseDelta = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
  50. // Scale input against the sensitivity setting and multiply that against the smoothing value.
  51. mouseDelta = Vector2.Scale (mouseDelta, new Vector2 (sensitivity.x * smoothing.x, sensitivity.y * smoothing.y));
  52. // Interpolate mouse movement over time to apply smoothing delta.
  53. _smoothMouse.x = Mathf.Lerp (_smoothMouse.x, mouseDelta.x, 1f / smoothing.x);
  54. _smoothMouse.y = Mathf.Lerp (_smoothMouse.y, mouseDelta.y, 1f / smoothing.y);
  55. // Find the absolute mouse movement value from point zero.
  56. _mouseAbsolute += _smoothMouse;
  57. // Clamp and apply the local x value first, so as not to be affected by world transforms.
  58. if (clampInDegrees.x < 360)
  59. _mouseAbsolute.x = Mathf.Clamp (_mouseAbsolute.x, -clampInDegrees.x * 0.5f, clampInDegrees.x * 0.5f);
  60. var xRotation = Quaternion.AngleAxis (-_mouseAbsolute.y, targetOrientation * Vector3.right);
  61. transform.localRotation = xRotation;
  62. // Then clamp and apply the global y value.
  63. if (clampInDegrees.y < 360)
  64. _mouseAbsolute.y = Mathf.Clamp (_mouseAbsolute.y, -clampInDegrees.y * 0.5f, clampInDegrees.y * 0.5f);
  65. transform.localRotation *= targetOrientation;
  66. // If there's a character body that acts as a parent to the camera
  67. if (characterBody) {
  68. var yRotation = Quaternion.AngleAxis (_mouseAbsolute.x, characterBody.transform.up);
  69. characterBody.transform.localRotation = yRotation;
  70. characterBody.transform.localRotation *= targetCharacterOrientation;
  71. } else {
  72. var yRotation = Quaternion.AngleAxis (_mouseAbsolute.x, transform.InverseTransformDirection (Vector3.up));
  73. transform.localRotation *= yRotation;
  74. }
  75. }
  76. void FixedUpdate(){
  77. if (Input.GetKey (rightKey)) {
  78. speedX += acceleration * Time.deltaTime;
  79. }
  80. else if (Input.GetKey (leftKey)) {
  81. speedX -= acceleration * Time.deltaTime;
  82. }
  83. if (Input.GetKey (backKey)) {
  84. speedZ -= acceleration * Time.deltaTime;
  85. } else if (Input.GetKey (fwdKey)) {
  86. speedZ += acceleration * Time.deltaTime;
  87. }
  88. speedX = Mathf.Lerp( speedX,0,dampingSpeed * Time.deltaTime);
  89. speedZ = Mathf.Lerp( speedZ,0,dampingSpeed * Time.deltaTime);
  90. speedX = Mathf.Clamp( speedX,-maxSpeed*Time.deltaTime, maxSpeed*Time.deltaTime);
  91. speedZ = Mathf.Clamp( speedZ,-maxSpeed*Time.deltaTime, maxSpeed*Time.deltaTime);
  92. transform.position = transform.TransformPoint( new Vector3( speedX,0,speedZ) );
  93. }
  94. }