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 Controls : MonoBehaviour
- {
- private float cameraSpeed = 0.001f;
- private float cameraHorizontal = 0f;
- private float cameraVertical = 0f;
- private float linearSpeedLimit = 1f;
- private float cameraDistance = 5f;
- // Start is called before the first frame update
- void Start()
- {
- Debug.Log("Hello World");
- Debug.Log(Input.GetAxis("Horizontal"));
- linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown("space")) //Display Debug
- {
- Debug.Log("Horizontal force: "+cameraHorizontal+". Vertical force: "+cameraVertical+".");
- Debug.Log("Linear Speed Limit: "+linearSpeedLimit);
- Debug.Log("Camera Distance from Tiles: "+cameraDistance);
- Debug.Log(transform.position[1]);
- }
- if (Input.mouseScrollDelta.y > 0f) //if mousewheel up then zoom in by changing camera size
- {
- linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
- cameraDistance -= (cameraSpeed*GetComponent<Camera>().orthographicSize/0.01f);
- GetComponent<Camera>().orthographicSize = cameraDistance;
- }
- else if (Input.mouseScrollDelta.y < 0f) //if mousewheel down then zoom out by changing camera size
- {
- linearSpeedLimit = GetComponent<Camera>().orthographicSize/5f;
- cameraDistance += (cameraSpeed*GetComponent<Camera>().orthographicSize/0.01f);
- GetComponent<Camera>().orthographicSize = cameraDistance;
- }
- if (cameraDistance < 1) //limit how close the camera can get
- {
- cameraDistance = 1;
- GetComponent<Camera>().orthographicSize = cameraDistance;
- }
- if (cameraDistance > 20) //limit how far the camera can get
- {
- cameraDistance = 20;
- GetComponent<Camera>().orthographicSize = cameraDistance;
- }
- if (Input.GetKey("w")) //if "w" then move camera up
- cameraVertical += cameraDistance;
- if (Input.GetKey("s"))//if "s" then move camera down
- cameraVertical -= cameraDistance;
- if (Input.GetKey("a"))//if "a" then move camera left
- cameraHorizontal -= cameraDistance;
- if (Input.GetKey("d"))//if "d" then move camera right
- cameraHorizontal += cameraDistance;
- if (cameraHorizontal > 0.1 * linearSpeedLimit) //Max camera speeds
- cameraHorizontal = 0.1f * linearSpeedLimit;
- if (cameraHorizontal < -0.1 * linearSpeedLimit)
- cameraHorizontal = -0.1f * linearSpeedLimit;
- if (cameraVertical > 0.1 * linearSpeedLimit)
- cameraVertical = 0.1f * linearSpeedLimit;
- if (cameraVertical < -0.1 * linearSpeedLimit)
- cameraVertical = -0.1f * linearSpeedLimit;
- //actual translate function
- transform.Translate(cameraHorizontal, cameraVertical, 0);
- transform.localScale = new Vector3(cameraDistance, cameraDistance, 1f);
- //Maxxing out position
- if (transform.position[1] > 44)//Y Position top
- transform.position = new Vector3(transform.position[0], 44, -1);
- else if(transform.position[1] < -23)//Y Position bottom
- transform.position = new Vector3(transform.position[0], -23, -1);
- if (transform.position[0] > 20)//X Position top
- transform.position = new Vector3(20, transform.position[1], -1);
- else if(transform.position[0] < -70)//X Position bottom
- transform.position = new Vector3(-70, transform.position[1], -1);
- if (!Input.GetKey("d")&&!Input.GetKey("a")) //is D and A are not pressed
- {
- if (cameraHorizontal > 0)//Camera smoothing factors
- cameraHorizontal -= 0.001f * linearSpeedLimit;
- if (cameraHorizontal < 0)//Camera smoothing factors
- cameraHorizontal += 0.001f * linearSpeedLimit;
- if (cameraHorizontal < 0.001 * linearSpeedLimit*5 && cameraHorizontal > -0.001 * linearSpeedLimit*5)//Camera stop
- cameraHorizontal = 0;
- }
- if (!Input.GetKey("w")&&!Input.GetKey("s"))//is W and S are not pressed
- {
- if (cameraVertical > 0)//Camera smoothing factors
- cameraVertical -= 0.001f * linearSpeedLimit;
- if (cameraVertical < 0)//Camera smoothing factors
- cameraVertical += 0.001f * linearSpeedLimit;
- if (cameraVertical < 0.001 * linearSpeedLimit*5 && cameraVertical > -0.001 * linearSpeedLimit*5)//Camera stop
- cameraVertical = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement