A free(ish) camera

I’ve just uploaded my latest version of Hex, delivering on my promise to free the camera. Sort of. I decided since there isn’t a whole lot to see in the game world, it didn’t make sense to have a totally free camera. Instead, I opted for a camera that runs on a track, and can zoom in and out. It also always stares at the middle of the board. It can’t zoom in and out forever, since there’s not a lot to see, but it can spin forever, which can be fun. The CameraController script (shown below), relies on input from the arrow keys to control the camera. Feel free to use it for non-commercial purposes, but let me know if you do, because I’d love to hear about it!

As a disclaimer, for whatever reason, the camera is very choppy on the web player, but it isn’t on my machine. I’ve opened issue #29 to track this.

 

//-----------------------------------------------------------------------
// <copyright file="CameraController.cs" company="Pete Biencourt">
//     Copyright (c) Pete Biencourt. All rights reserved.
// </copyright>
// <author>Pete Biencourt</author>
//-----------------------------------------------------------------------

namespace HexGame
{
    using UnityEngine;

    /// <summary>
    /// Camera controller
    /// </summary>
    public class CameraController : MonoBehaviour
    {
        /// <summary>
        /// Track radius
        /// </summary>
        public float TrackRadius = 7.5f;

        /// <summary>
        /// Rotation step size
        /// </summary>
        public float RotationStepSize = 0.05f;

        /// <summary>
        /// Zoom step size
        /// </summary>
        public float ZoomStepSize = 0.1f;

        /// <summary>
        /// Minimum camera height
        /// </summary>
        public float MinYValue = 2f;

        /// <summary>
        /// Maximum camera height
        /// </summary>
        public float MaxYValue = 10f;

        /// <summary>
        /// Tracks the value of the radian parameter for the camera's track
        /// </summary>
        private float parameterValue;

        /// <summary>
        /// Starts the camera
        /// </summary>
        void Start()
        {
            this.parameterValue = 0f;
            this.UpdateParameterAndGetNewPosition(0f);
        }

        /// <summary>
        /// Updates the camera
        /// </summary>
        public void Update()
        {
            if (Input.GetKey(KeyCode.RightArrow))
            {
                this.UpdateParameterAndGetNewPosition(this.RotationStepSize);
            }

            if (Input.GetKey(KeyCode.LeftArrow))
            {
                this.UpdateParameterAndGetNewPosition((-1) * this.RotationStepSize);
            }

            if (Input.GetKey(KeyCode.UpArrow) && !Input.GetKey(KeyCode.DownArrow))
            {
                this.Zoom((-1) * this.ZoomStepSize);
            }

            if (!Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
            {
                this.Zoom(this.ZoomStepSize);
            }

            Camera.main.transform.LookAt(new Vector3(0f, 0f, 0f));
        }

        /// <summary>
        /// Handles keeping the camera on its track
        /// </summary>
        /// <param name="parameterDelta">How much to move it by</param>
        private void UpdateParameterAndGetNewPosition(float parameterDelta)
        {
            this.parameterValue += parameterDelta;
            Vector3 pos = Camera.main.transform.position;
            pos.z = (-1) * this.TrackRadius * Mathf.Cos(this.parameterValue);
            pos.x = (-1) * this.TrackRadius * Mathf.Sin(this.parameterValue);
            Camera.main.transform.position = pos;
        }

        /// <summary>
        /// Zooms the camera in or out
        /// </summary>
        /// <param name="zoomDelta">How much to zoom</param>
        private void Zoom(float zoomDelta)
        {
            Vector3 pos = Camera.main.transform.position;
            pos.y += zoomDelta;
            if (pos.y < this.MinYValue)
            {
                pos.y = this.MinYValue;
            }
            if (pos.y > this.MaxYValue)
            {
                pos.y = this.MaxYValue;
            }
            Camera.main.transform.position = pos;
        }
    }
}
 

piebie

 

Leave a Reply

Your email address will not be published. Required fields are marked *