Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
156
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 RTSCamera : MonoBehaviour
  6. {
  7. //Component Assignment
  8. Camera cam;
  9.  
  10. [Header("Camera Variables")]
  11. public float panSpeed = 5;
  12. public float rotateSpeed = 50;
  13. public float rotateAmount = 50;
  14.  
  15. float panDetect = 15f;
  16. Quaternion cameraRotation;
  17. float minCameraHeight = 2f;
  18. float maxCameraHeight = 10f;
  19.  
  20.  
  21. private void Start()
  22. {
  23. //Initialise Component
  24. cam = GetComponentInChildren<Camera>();
  25. cameraRotation = cam.transform.rotation;
  26. }
  27.  
  28. private void Update()
  29. {
  30. MoveCamera();
  31. RotateCamera();
  32.  
  33. if(Input.GetKeyDown(KeyCode.Space))
  34. {
  35. cam.transform.rotation = cameraRotation;
  36. }
  37. }
  38.  
  39. void MoveCamera() //Handles basic panning
  40. {
  41. float moveX = cam.transform.position.x;
  42. float moveY = cam.transform.position.y;
  43. float moveZ = cam.transform.position.z;
  44.  
  45. float xPos = Input.mousePosition.x;
  46. float yPos = Input.mousePosition.y;
  47.  
  48. if (Input.GetKey(KeyCode.A) || (xPos > 0 && xPos < panDetect))
  49. {
  50. moveX -= panSpeed * Time.deltaTime;
  51. }
  52. else if (Input.GetKey(KeyCode.D) || (xPos < Screen.width && xPos > Screen.width - panDetect))
  53. {
  54. moveX += panSpeed * Time.deltaTime;
  55. }
  56.  
  57. if (Input.GetKey(KeyCode.W) || yPos < Screen.height && yPos > Screen.height - panDetect)
  58. {
  59. moveZ += panSpeed * Time.deltaTime;
  60. }
  61. else if (Input.GetKey(KeyCode.S) || yPos > 0 && yPos < panSpeed)
  62. {
  63. moveZ -= panSpeed * Time.deltaTime;
  64. }
  65.  
  66. //Camera Height
  67. moveY -= Input.GetAxis("Mouse ScrollWheel") * (panSpeed * 20 * Time.deltaTime);
  68. moveY = Mathf.Clamp(moveY, minCameraHeight, maxCameraHeight);
  69.  
  70. Vector3 newPos = new Vector3(moveX, moveY, moveZ);
  71. cam.transform.position = newPos;
  72. }
  73.  
  74. void RotateCamera()
  75. {
  76. Vector3 origin = cam.transform.eulerAngles;
  77. Vector3 destination = origin;
  78.  
  79. if (Input.GetMouseButton(2)) //Hold MiddleMouse to Rotate
  80. {
  81. destination.x -= Input.GetAxis("Mouse Y") * rotateAmount * Time.deltaTime;
  82. destination.y += Input.GetAxis("Mouse X") * rotateAmount * Time.deltaTime;
  83. }
  84.  
  85. if (destination != origin)
  86. {
  87. cam.transform.eulerAngles = Vector3.MoveTowards(origin, destination, rotateSpeed * Time.deltaTime);
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement