Created
December 9, 2023 08:25
-
-
Save tombasche/ddf7be4a69e27f26ed665ad5cf5c2bf9 to your computer and use it in GitHub Desktop.
cinemachine top down camera
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
using UnityEngine; | |
using Cinemachine; | |
public class CameraController : MonoBehaviour | |
{ | |
[SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera; | |
private const float MIN_FOLLOW_Y_OFFSET = 2f; | |
private const float MAX_FOLLOW_Y_OFFSET = 12f; | |
private CinemachineTransposer cinemachineTransposer; | |
private Vector3 targetFollowOffset; | |
private void Start() | |
{ | |
cinemachineTransposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>(); | |
targetFollowOffset = cinemachineTransposer.m_FollowOffset; | |
} | |
private void Update() | |
{ | |
Vector3 inputMoveDirection = new Vector3(0,0,0); | |
if (Input.GetKey(KeyCode.W)) | |
{ | |
inputMoveDirection.z = +1f; | |
} | |
if (Input.GetKey(KeyCode.S)) | |
{ | |
inputMoveDirection.z = -1f; | |
} | |
if (Input.GetKey(KeyCode.A)) | |
{ | |
inputMoveDirection.x = -1f; | |
} | |
if (Input.GetKey(KeyCode.D)) | |
{ | |
inputMoveDirection.x = +1f; | |
} | |
float moveSpeed = 10f; | |
Vector3 moveVector = transform.forward * inputMoveDirection.z + transform.right * inputMoveDirection.x; | |
transform.position += moveVector * moveSpeed * Time.deltaTime; | |
Vector3 rotationVector = new Vector3(0, 0, 0); | |
if (Input.GetKey(KeyCode.Q)) | |
{ | |
rotationVector.y = +1f; | |
} | |
if (Input.GetKey(KeyCode.E)) | |
{ | |
rotationVector.y = -1f; | |
} | |
float rotationSpeed = 100f; | |
transform.eulerAngles += rotationVector * rotationSpeed * Time.deltaTime; | |
float zoomAmount = 1f; | |
if (Input.mouseScrollDelta.y > 0) | |
{ | |
targetFollowOffset.y -= zoomAmount; | |
} | |
if (Input.mouseScrollDelta.y < 0) | |
{ | |
targetFollowOffset.y += zoomAmount; | |
} | |
targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, MIN_FOLLOW_Y_OFFSET, MAX_FOLLOW_Y_OFFSET); | |
float zoomSpeed = 5f; | |
cinemachineTransposer.m_FollowOffset = | |
Vector3.Lerp(cinemachineTransposer.m_FollowOffset, targetFollowOffset, Time.deltaTime * zoomSpeed); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment