Files
CityBuilder/Assets/Scripts/CameraController.cs

125 lines
4.3 KiB
C#
Raw Normal View History

2025-07-07 20:59:04 +01:00
using UnityEngine;
using UnityEngine.InputSystem;
public class CameraController : MonoBehaviour
{
public float moveSpeed;
public float minXRot;
public float maxXRot;
public float minZoom;
public float maxZoom;
public float zoomSpeed;
public float rotateSpeed;
private float curXRot;
private float curZoom;
private Camera cam;
public Vector3 minBounds;
public Vector3 maxBounds;
2025-07-07 20:59:04 +01:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
cam = Camera.main;
curZoom = cam.transform.localPosition.y;
curXRot = -50;
}
// Update is called once per frame
void Update()
{
HandleZoom();
HandleRotation();
HandleMovement();
HandleEdges(); // Ensure camera stays within bounds
2025-07-07 20:59:04 +01:00
RotateGameObject();
}
public void HandleZoom()
{
//getting the scrool wheel value
curZoom += Input.GetAxis("Mouse ScrollWheel") * -zoomSpeed;
//clamping it between min and max zoom
curZoom = Mathf.Clamp(curZoom, minZoom, maxZoom);
//applying it to the camera
cam.transform.localPosition = Vector3.up * curZoom;
}
public void HandleRotation()
{
//camera look
if (Input.GetMouseButton(1))
{
//getting the mouse movement
float x = Input.GetAxis("Mouse X");
float y = Input.GetAxis("Mouse Y");
//rotating the camera based on the mouse movement
curXRot += -y * rotateSpeed;
//clamping the x rotation to prevent flipping
curXRot = Mathf.Clamp(curXRot, minXRot, maxXRot);
//applying the rotation to the camera
transform.eulerAngles = new Vector3(curXRot, transform.eulerAngles.y + (x * rotateSpeed), 0.0f);
}
}
public void HandleMovement()
{
//movement of the camera forward
Vector3 forward = cam.transform.forward;
//prevent vertical movement
forward.y = 0.0f;
//normalize the forward vector to prevent faster movement when moving diagonally
forward.Normalize();
//movement of the camera right
Vector3 right = cam.transform.right;
//getting the horizontal and vertical input from keyboard
float moveX = Input.GetAxisRaw("Horizontal");
float moveZ = Input.GetAxisRaw("Vertical");
//get local direction based on input
Vector3 dir = forward * moveZ + right * moveX;
//normalize the direction vector to prevent faster movement when moving diagonally
dir.Normalize();
//apply the movement speed and delta time to the direction vector
dir *= moveSpeed * Time.deltaTime;
//apply the movement to the camera anchor
transform.position += dir;
}
public void HandleEdges()
{
// Clamp the camera position within the defined bounds
Vector3 clampedPosition = transform.position;
clampedPosition.x = Mathf.Clamp(clampedPosition.x, minBounds.x, maxBounds.x);
clampedPosition.z = Mathf.Clamp(clampedPosition.z, minBounds.z, maxBounds.z);
transform.position = clampedPosition;
}
2025-07-07 20:59:04 +01:00
public void RotateGameObject()
{
if (!BuildingPlacement.Instance.currentlyPlacing)
{
return; // Do nothing if not currently placing a building
}
// Check for input to rotate the game object
GameObject placementObject = BuildingPlacement.Instance.placementPrefab;
// Rotate left or right based on input
if (placementObject != null)
{
//if (Input.GetButtonDown("RotateLeft"))
if (Keyboard.current.qKey.wasPressedThisFrame)
{
// Rotate the game object by -90 degrees around the Y axis
placementObject.transform.Rotate(0, -90, 0);
BuildingPlacement.Instance.curIndicatorRotation = placementObject.transform.rotation;
}
//else if (Input.GetButtonDown("RotateRight"))
else if (Keyboard.current.eKey.wasPressedThisFrame)
{
// Rotate the game object by 90 degrees around the Y axis
placementObject.transform.Rotate(0, 90, 0);
BuildingPlacement.Instance.curIndicatorRotation = placementObject.transform.rotation;
}
}
}
}