Last active
April 21, 2020 15:00
-
-
Save saturngamesss/981b6a820e7d8621dcd314507a8d1cb7 to your computer and use it in GitHub Desktop.
Simple camera move with zooming using mouse wheel code for Unity Engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//************** REAL GAMES STUDIO *************** | |
//************************************************ | |
//realgamesss.weebly.com | |
//gamejolt.com/@Real_Game | |
//realgamesss.newgrounds.com/ | |
//real-games.itch.io/ | |
//youtube.com/channel/UC_Adg-mo-IPg6uLacuQCZCQ | |
//************************************************ | |
using UnityEngine; | |
public class CameraMove : MonoBehaviour | |
{ | |
[Header("Camera")] | |
public float speed = 10.0f; | |
Vector3 camPos; | |
[Header("Zoom")] | |
public float minZoom = 5.0f; | |
public float maxZoom = 250.0f; | |
float zoom; | |
void Update() | |
{ | |
Move(); | |
transform.position = camPos; | |
camPos = transform.position; | |
Zoom(); | |
zoom = Mathf.Clamp(zoom, minZoom, maxZoom); | |
GetComponent<Camera>().orthographicSize = zoom; | |
} | |
void Move() | |
{ | |
if (Input.GetKey(KeyCode.W)) | |
{ | |
camPos.y += speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.S)) | |
{ | |
camPos.y -= speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.A)) | |
{ | |
camPos.x -= speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.D)) | |
{ | |
camPos.x += speed * Time.deltaTime; | |
} | |
} | |
void Zoom() | |
{ | |
if (Input.GetKey(KeyCode.KeypadMinus)) | |
{ | |
zoom -= speed * Time.deltaTime; | |
} | |
if (Input.GetKey(KeyCode.KeypadPlus)) | |
{ | |
zoom += speed * Time.deltaTime; | |
} | |
if (Input.mouseScrollDelta.y > 0) | |
{ | |
zoom -= speed * Time.deltaTime * 10f; | |
} | |
if (Input.mouseScrollDelta.y < 0) | |
{ | |
zoom += speed * Time.deltaTime * 10f; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment