- Home /
This camera controller is overriding it's default position when the boundary check is enabled?
I'm working on a camera controller for a 2D top-down grid. This works almost perfectly, -however-, I've discovered a discrepancy in that it's boundary check resets it's default position to zero, and ignores whatever position the camera is assigned at first.
I've been trying to figure this out, but I have been stumped. The problem is occurring in LateUpdate, and any position the camera starts with will revert to X = 0, Y = height, Z = 0.
How can I correct this?
using UnityEngine;
/// <summary>
/// Top Down Camera Script - Orthographic View
/// <para>Sets up a top down camera, with optional zooming, and controlled by the horizontal and veritcal axis.</para>
/// <para>Designed for free scrolling of a map or set defined area.</para>
/// </summary>
public class CameraController : MonoBehaviour
{
public Bounds visibleBounds;
public Transform boundsMarker;
public float activeSpeed = 0;
public float fastMulti = 0.5f;
public float keyScroll = 0.25f;
public float zoomMax = 25;
public float zoomMin = 50;
public float zoomControl = 0;
public float zoomSensitivity = 5f;
public bool zoomEnabled = true;
public bool cameraReady = false;
public bool clampBounds = false;
Vector2 cameraSizeWorld;
Vector2 halfSize;
private Camera _camera;
private AudioListener _listener;
private Transform _trans;
private void Awake()
{
_trans = GetComponent<Transform>();
_camera = GetComponent<Camera>();
_listener = GetComponent<AudioListener>();
}
public void setCamEnabledState(bool state)
{
_camera.enabled = state;
_listener.enabled = state;
}
void FixedUpdate()
{
if (cameraReady)
{
CheckKeyboardScroll();
if (zoomEnabled)
{
checkZoomState();
}
updateExtents();
Debug.Log("Position at Update: " + _trans.position);
}
}
/// <summary>
/// <para>Listen for the horizontal and vertical camera axis.</para>
/// <para>This is designed to be used on a free map, not following a target.</para>
/// </summary>
private void CheckKeyboardScroll()
{
if (!Input.GetKey(KeyCode.LeftShift))
{
activeSpeed = keyScroll;
} else
{
activeSpeed = fastMulti;
}
float _x = Input.GetAxis("Horizontal") * activeSpeed;
float _z = Input.GetAxis("Vertical") * activeSpeed;
_trans.position = _trans.position + new Vector3(_x * Time.deltaTime, 0, _z * Time.deltaTime);
}
/// <summary>
/// Position camera to fit the grid size, using orthographic value to zoom appropriately.
/// </summary>
/// <param name="boardSize"></param>
public void positionCamera(float boardSize)
{
if (_camera)
{
Vector3 pos = _trans.position;
pos.z = -0.90f;
_camera.orthographicSize = (boardSize * Screen.height / Screen.width * 0.5f) + 1.85f;
_trans.position = pos;
}
cameraReady = true;
}
/// <summary>
/// Check if the resolution of the camera has changed
/// </summary>
private void updateExtents()
{
if (boundsMarker)
{
visibleBounds = boundsMarker.GetComponent<BoxCollider>().bounds;
cameraSizeWorld = _camera.ViewportToWorldPoint(Vector2.one) - _camera.ViewportToWorldPoint(Vector2.zero);
halfSize = new Vector2(Mathf.Floor(cameraSizeWorld.x / 2), Mathf.Floor(cameraSizeWorld.y / 2));
}
}
/// <summary>
/// <para>If zoom controls are enabled, allow zooming the orthographic size.</para>
/// </summary>
private void checkZoomState()
{
zoomControl = _camera.orthographicSize;
zoomControl -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity;
zoomControl = Mathf.Clamp(zoomControl, zoomMin, zoomMax);
_camera.orthographicSize = zoomControl;
}
/// <summary>
/// Keep the camera clamped to a certain map range, limiting the area that can be moved in.
/// </summary>
private void LateUpdate()
{
if (boundsMarker && clampBounds)
{
Vector3 v3 = transform.position;
v3.x = Mathf.Clamp(v3.x, visibleBounds.min.x + halfSize.x, visibleBounds.max.x - halfSize.x);
v3.z = Mathf.Clamp(v3.z, visibleBounds.min.z + halfSize.y, visibleBounds.max.z - halfSize.y);
transform.position = v3;
}
}
}
Comment