Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RTSCamera : MonoBehaviour
- {
- //Component Assignment
- Camera cam;
- [Header("Camera Variables")]
- public float panSpeed = 5;
- public float rotateSpeed = 50;
- public float rotateAmount = 50;
- float panDetect = 15f;
- Quaternion cameraRotation;
- float minCameraHeight = 2f;
- float maxCameraHeight = 10f;
- private void Start()
- {
- //Initialise Component
- cam = GetComponentInChildren<Camera>();
- cameraRotation = cam.transform.rotation;
- }
- private void Update()
- {
- MoveCamera();
- RotateCamera();
- if(Input.GetKeyDown(KeyCode.Space))
- {
- cam.transform.rotation = cameraRotation;
- }
- }
- void MoveCamera() //Handles basic panning
- {
- float moveX = cam.transform.position.x;
- float moveY = cam.transform.position.y;
- float moveZ = cam.transform.position.z;
- float xPos = Input.mousePosition.x;
- float yPos = Input.mousePosition.y;
- if (Input.GetKey(KeyCode.A) || (xPos > 0 && xPos < panDetect))
- {
- moveX -= panSpeed * Time.deltaTime;
- }
- else if (Input.GetKey(KeyCode.D) || (xPos < Screen.width && xPos > Screen.width - panDetect))
- {
- moveX += panSpeed * Time.deltaTime;
- }
- if (Input.GetKey(KeyCode.W) || yPos < Screen.height && yPos > Screen.height - panDetect)
- {
- moveZ += panSpeed * Time.deltaTime;
- }
- else if (Input.GetKey(KeyCode.S) || yPos > 0 && yPos < panSpeed)
- {
- moveZ -= panSpeed * Time.deltaTime;
- }
- //Camera Height
- moveY -= Input.GetAxis("Mouse ScrollWheel") * (panSpeed * 20 * Time.deltaTime);
- moveY = Mathf.Clamp(moveY, minCameraHeight, maxCameraHeight);
- Vector3 newPos = new Vector3(moveX, moveY, moveZ);
- cam.transform.position = newPos;
- }
- void RotateCamera()
- {
- Vector3 origin = cam.transform.eulerAngles;
- Vector3 destination = origin;
- if (Input.GetMouseButton(2)) //Hold MiddleMouse to Rotate
- {
- destination.x -= Input.GetAxis("Mouse Y") * rotateAmount * Time.deltaTime;
- destination.y += Input.GetAxis("Mouse X") * rotateAmount * Time.deltaTime;
- }
- if (destination != origin)
- {
- cam.transform.eulerAngles = Vector3.MoveTowards(origin, destination, rotateSpeed * Time.deltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement