Advertisement
Beesafree

Camera Contrals

Apr 15th, 2021
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Controls : MonoBehaviour
  6. {
  7.     private float cameraSpeed = 0.001f;
  8.     private float cameraHorizontal = 0f;
  9.     private float cameraVertical = 0f;
  10.     private float linearSpeedLimit = 1f;
  11.     private float cameraDistance = 5f;
  12.    
  13.     // Start is called before the first frame update
  14.     void Start()
  15.     {
  16.         Debug.Log("Hello World");
  17.         Debug.Log(Input.GetAxis("Horizontal"));
  18.         linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
  19.     }
  20.  
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.        
  25.         if (Input.GetKeyDown("space")) //Display Debug
  26.         {
  27.             Debug.Log("Horizontal force: "+cameraHorizontal+". Vertical force: "+cameraVertical+".");
  28.             Debug.Log("Linear Speed Limit: "+linearSpeedLimit);
  29.             Debug.Log("Camera Distance from Tiles: "+cameraDistance);
  30.             Debug.Log(transform.position[1]);
  31.         }
  32.        
  33.         if (Input.mouseScrollDelta.y > 0f) //if mousewheel up then zoom in by changing camera size
  34.         {
  35.             linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
  36.             cameraDistance -= (cameraSpeed*GetComponent<Camera>().orthographicSize/0.01f);
  37.             GetComponent<Camera>().orthographicSize = cameraDistance;
  38.         }
  39.         else if (Input.mouseScrollDelta.y < 0f) //if mousewheel down then zoom out by changing camera size
  40.         {
  41.             linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
  42.             cameraDistance += (cameraSpeed*GetComponent<Camera>().orthographicSize/0.01f);
  43.             GetComponent<Camera>().orthographicSize = cameraDistance;
  44.         }
  45.            
  46.         if (cameraDistance < 1) //limit how close the camera can get
  47.         {
  48.             cameraDistance = 1;
  49.             GetComponent<Camera>().orthographicSize = cameraDistance;
  50.         }
  51.         if (cameraDistance > 20) //limit how far the camera can get
  52.         {
  53.             cameraDistance = 20;
  54.             GetComponent<Camera>().orthographicSize = cameraDistance;
  55.         }
  56.        
  57.         if (Input.GetKey("w")) //if "w" then move camera up
  58.             cameraVertical += cameraDistance;
  59.        
  60.         if (Input.GetKey("s"))//if "s" then move camera down
  61.             cameraVertical -= cameraDistance;
  62.        
  63.         if (Input.GetKey("a"))//if "a" then move camera left
  64.             cameraHorizontal -= cameraDistance;
  65.  
  66.         if (Input.GetKey("d"))//if "d" then move camera right
  67.             cameraHorizontal += cameraDistance;
  68.        
  69.         if (cameraHorizontal > 0.1 * linearSpeedLimit) //Max camera speeds
  70.             cameraHorizontal = 0.1f * linearSpeedLimit;
  71.         if (cameraHorizontal < -0.1 * linearSpeedLimit)
  72.             cameraHorizontal = -0.1f * linearSpeedLimit;
  73.         if (cameraVertical > 0.1 * linearSpeedLimit)
  74.             cameraVertical = 0.1f * linearSpeedLimit;
  75.         if (cameraVertical < -0.1 * linearSpeedLimit)
  76.             cameraVertical = -0.1f * linearSpeedLimit;
  77.        
  78.         //actual translate function
  79.         transform.Translate(cameraHorizontal, cameraVertical, 0);
  80.         transform.localScale = new Vector3(cameraDistance, cameraDistance, 1f);
  81.        
  82.         //Maxxing out position
  83.         if (transform.position[1] > 44)//Y Position top
  84.             transform.position = new Vector3(transform.position[0], 44, -1);
  85.         else if(transform.position[1] < -23)//Y Position bottom
  86.             transform.position = new Vector3(transform.position[0], -23, -1);
  87.         if (transform.position[0] > 20)//X Position top
  88.             transform.position = new Vector3(20, transform.position[1], -1);
  89.         else if(transform.position[0] < -70)//X Position bottom
  90.             transform.position = new Vector3(-70, transform.position[1], -1);
  91.        
  92.         if (!Input.GetKey("d")&&!Input.GetKey("a")) //is D and A are not pressed
  93.         {
  94.             if (cameraHorizontal > 0)//Camera smoothing factors
  95.                 cameraHorizontal -= 0.001f * linearSpeedLimit;
  96.             if (cameraHorizontal < 0)//Camera smoothing factors
  97.                 cameraHorizontal += 0.001f * linearSpeedLimit;  
  98.             if (cameraHorizontal < 0.001 * linearSpeedLimit*5 && cameraHorizontal > -0.001 * linearSpeedLimit*5)//Camera stop
  99.                 cameraHorizontal = 0;
  100.         }  
  101.         if (!Input.GetKey("w")&&!Input.GetKey("s"))//is W and S are not pressed
  102.         {
  103.             if (cameraVertical > 0)//Camera smoothing factors
  104.                 cameraVertical -= 0.001f * linearSpeedLimit;
  105.             if (cameraVertical < 0)//Camera smoothing factors
  106.                 cameraVertical += 0.001f * linearSpeedLimit;  
  107.             if (cameraVertical < 0.001 * linearSpeedLimit*5 && cameraVertical > -0.001 * linearSpeedLimit*5)//Camera stop
  108.                 cameraVertical = 0;
  109.         }
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement