TODO causes major rewrite

I have had a TODO in my camera controller script for awhile now to get the camera zoom and rotation working with the gamepads right stick (thus answering the question of whether or not isInDirectMode should be a static).

I’ve been using a tweaked version of the FreeLookCam.cs from the standard assets, to allow zoom and rotation on the mouse wheel and have been putting off thinking about the Direct Mode equiv until this lecture. I ended up spending a good few hours on it and dumped the FreeLookCam altogether, replacing it with this script below:-

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour {

    public Transform target;

    public Vector3 offset;
    public float zoomSpeed = 4f;
    public float minZoom = 5f;
    public float maxZoom = 15f;
    public float pitch = 2f;

    public float yawSpeed = 100f;

    private float currentZoom = 10f;
    private float currentYaw = 0f;
	
	void Update () {

        if (PlayerMovement.isInDirectMode)
        {
            // Handle Direct Movement

            // TODO - Player occasionally gets stuck facing the camera in this mode, work out why and fix.

            currentZoom -= Input.GetAxis("Vertical R"); // "Vertical R" being the right stick on the 360 controller
            currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);
            currentYaw -= Input.GetAxis("Horizontal R") * yawSpeed * Time.deltaTime;
        }
        else
        {
            // Handle Mouse Movement

            currentZoom -= Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
            currentZoom = Mathf.Clamp(currentZoom, minZoom, maxZoom);

            if (Input.GetMouseButton(2))
            {
                currentYaw -= Input.GetAxis("Mouse X") * yawSpeed * Time.deltaTime;
            }
            
        }

    }
	
	// Update is called once per frame
	void LateUpdate () {
        transform.position = target.position - offset * currentZoom;
        transform.LookAt(target.position + Vector3.up * pitch);

        transform.RotateAround(target.position, Vector3.up, currentYaw);
	}
}

I had to tweak some InputManager values to make it work but overall, although there are still a couple of kinks in it (such as the player occasionally rotating with the camera) I am much happier with it and I don’t need to have the Camera Rig and Pivot that FreeLookCam required.

tl;dr - Its very satisfying to finally implement something that’s been on the list for awhile, but have been avoiding…

1 Like

Privacy & Terms