using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameCamera : MonoBehaviour { public float smooth; public float moveSpeed; public Transform tablePlane; private Vector3 currentVelocity; private Rect tableBounds; private void Awake() { if (CameraController.Instance != null) CameraController.Instance.gameObject.SetActive(false); } void Start() { TableBoundsSetup(); } private void TableBoundsSetup() { float width = tablePlane.lossyScale.x * 2 * 5; float height = tablePlane.lossyScale.z * 2 * 5; float x = tablePlane.position.x - width / 2; float y = tablePlane.position.z - height / 2; tableBounds = new Rect(x, y, width, height); } void Update() { MoveCameraWithInput(); } private void MoveCameraWithInput() { Vector2 moveDir2 = GetAxisDirection(); //Hide circuit menu on camera move /*if (moveDir2 != Vector2.zero) { Circuit circuit = GameGUI.Instance.currentMenuCircuit; if (circuit != null) { circuit.Deselect(); } }*/ float moveX = -moveDir2.y; float moveZ = moveDir2.x; float x = transform.position.x + moveX; float z = transform.position.z + moveZ; Vector3 newMoveDir = Vector3.zero; if (x < tableBounds.x + tableBounds.width && x > tableBounds.x) newMoveDir.x = moveX; if (z < tableBounds.y + tableBounds.height && z > tableBounds.y) newMoveDir.z = moveZ; Vector3 newPosition = transform.position + newMoveDir * moveSpeed; MoveSmoothlyTo(newPosition); } private void OnDrawGizmos() { Gizmos.color = Color.red; Vector3 origin = new Vector3(tableBounds.x, 73, tableBounds.y); Vector3 c1 = origin + new Vector3(tableBounds.width, 0, 0); Vector3 c2 = origin + new Vector3(0, 0, tableBounds.height); Vector3 c3 = origin + new Vector3(tableBounds.width, 0, tableBounds.height); Gizmos.DrawLine(origin, c1); Gizmos.DrawLine(origin, c2); Gizmos.DrawLine(c1, c3); Gizmos.DrawLine(c2, c3); } private void MoveSmoothlyTo(Vector3 pos) { transform.position = Vector3.SmoothDamp(transform.position, pos, ref currentVelocity, smooth); } private Vector2 GetAxisDirection() { float x = Input.GetAxisRaw("Horizontal"); float y = Input.GetAxisRaw("Vertical"); return new Vector2(x, y); } }