Last active
March 16, 2019 15:26
-
-
Save Josef212/caaf4c49545ecda9b414c7797b438a42 to your computer and use it in GitHub Desktop.
Unity camera movement
This file contains hidden or 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
using UnityEngine; | |
public class CameraMovement : MonoBehaviour | |
{ | |
void Update() | |
{ | |
float movSpeed = m_cameraMovementSpeed; | |
float rotSpeed = m_cameraRotationSpeed; | |
if(Input.GetKey(KeyCode.LeftShift)) | |
{ | |
movSpeed *= 2.0f; | |
rotSpeed *= 2.0f; | |
} | |
if(Input.GetMouseButton(0)) | |
{ | |
Vector3 rotation = transform.eulerAngles; | |
if (rotation.x > 180.0f) rotation.x -= 360.0f; | |
rotation.y += (Input.GetAxis("Mouse X") * rotSpeed * Time.deltaTime); | |
rotation.x -= (Input.GetAxis("Mouse Y") * rotSpeed * Time.deltaTime); | |
rotation.x = Mathf.Clamp(rotation.x, -90.0f, 90.0f); | |
rotation.z = 0.0f; | |
transform.eulerAngles = rotation; | |
} | |
float horizontal = Input.GetAxis("Horizontal"); | |
float vertical = Input.GetAxis("Vertical"); | |
Vector3 movement = Vector3.zero; | |
if (horizontal != 0.0f || vertical != 0.0f) | |
{ | |
movement += (transform.right * horizontal); | |
movement += (transform.forward * vertical); | |
} | |
if(Input.GetKey(KeyCode.Q)) | |
{ | |
movement += Vector3.up; | |
} | |
if(Input.GetKey(KeyCode.E)) | |
{ | |
movement += Vector3.down; | |
} | |
if(movement != Vector3.zero) | |
{ | |
movement *= (movSpeed * Time.deltaTime); | |
transform.position += movement; | |
} | |
} | |
[SerializeField] private float m_cameraMovementSpeed = 10.0f; | |
[SerializeField] private float m_cameraRotationSpeed = 100.0f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment