Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CameraMovement : MonoBehaviour
  6. {
  7. public Transform target;
  8. public Transform parent;
  9.  
  10. public float distance = 5.0f;
  11. public float xSpeed = 120.0f;
  12. public float ySpeed = 120.0f;
  13.  
  14. public float yMinLimit = -20f;
  15. public float yMaxLimit = 80f;
  16.  
  17. public float distanceMin = .5f;
  18. public float distanceMax = 15f;
  19.  
  20. public bool isSwitchingTarget;
  21.  
  22. float x = 0.0f;
  23. float y = 0.0f;
  24.  
  25. Vector3 position;
  26.  
  27. // Use this for initialization
  28. void Start()
  29. {
  30. isSwitchingTarget = false;
  31. Vector3 angles = transform.eulerAngles;
  32. x = angles.y;
  33. y = angles.x;
  34. }
  35.  
  36. private void Update()
  37. {
  38. if (Input.GetMouseButton(0))
  39. {
  40. ChangeTargets(2f);
  41. }
  42. }
  43.  
  44. void LateUpdate()
  45. {
  46. if (target)
  47. {
  48. if (Input.GetMouseButton(1))
  49. {
  50. x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
  51. y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
  52. y = ClampAngle(y, yMinLimit, yMaxLimit);
  53. }
  54.  
  55. if (!isSwitchingTarget)
  56. {
  57. Quaternion rotation = Quaternion.Euler(y, x, 0);
  58.  
  59. //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
  60.  
  61. Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
  62. position = rotation * negDistance + target.position;
  63.  
  64. transform.rotation = rotation;
  65. transform.position = position;
  66. }
  67. }
  68.  
  69. }
  70.  
  71. public static float ClampAngle(float angle, float min, float max)
  72. {
  73. if (angle < -360F)
  74. angle += 360F;
  75. if (angle > 360F)
  76. angle -= 360F;
  77. return Mathf.Clamp(angle, min, max);
  78. }
  79.  
  80. public void ChangeTargets(float time)
  81. {
  82. RaycastHit hit;
  83. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  84. if (Physics.Raycast(ray, out hit))
  85. {
  86. Debug.Log(hit.collider.name);
  87. target = null;
  88. Vector3 newpos = hit.collider.transform.position;
  89. target = hit.collider.transform;
  90. }
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement