Camera Tweaking

First off, wonderful tweaks in this lecture. I was DEFINITELY irritated by the wonkiness when you were close to an enemy. It certainly is a preference thing and I’ll really play with it more later but the values here were pretty good. I leaned a tiny bit less snappy than he prefers but otherwise close.

However, something that has bothered me from the beginning. It truly feels like you should be able to orbit much higher at the top of screen. Hard to explain… I use inverted mouse for everything. I am one of those weirdos, so for me… pushing the mouse forward aims the camera down for me. And it seems like it is very clamped for how far forward while aiming down the camera will go (and a quick philosophical note here… describing the camera movement like this is EXACTLY why my brain prefers inverted mouse).

I’ve played with some settings and I can’t seem to affect it. Other custom cameras I’ve played with from controllers seem to not have this issue. I can’t remember none of them used Cinemachine or if one did. Another thing I noticed from other camera controllers is usually when you hit the ground when looking up the camera will sort of slide along the ground getting closer to the player while tilting the view up more (rather than just abruptly hitting the ground and that’s it… no camera aiming).

I intend to tinker more, but if anyone has any thoughts, please let me know. However, given how many times I ask something and then it gets stuck in my mind again so I tinker more and solved my issue… I may have it figured out by then :rofl:

OOH! I just also remembered my other issue. I’d still like at least SOME control over tilting the camera up and down in targeting mode. I am sure I am missing something obvious, but any help there would also be great. I get claustrophobic with our combat in targeting mode because I am mostly just body blocking my view of the enemy. I’d love to be able to tilt down a bit to see from above if needed.

Ah… duh… The height and radius of the rigs basically get my main question answered… Right down to the sliding up to the player (with a smaller radius). Vertical FOV helps as well.

So that is that… But my other concern about y axis in targeting mode is now the mystery.

EDIT: I’ve also noticed that the speed and acceleration/deceleration do not seem to do much of anything on these cameras. Weird.

EDIT 2: Adding a Camera Offset component and setting Y offset to 2 and then in Aim setting Tracked Object Offset to -2 makes the targeting camera THE SWEETNESS for me personally… But that is a huge preference thing and still would prefer just free y axis if possible. I think I’ve messed with every setting and it may just not be allowed for a targeting camera.

I’m a little short on time tonight, but here’s my camera controller, which lets me zoom in/out, and orbit the target. Combine this with a Cinemachine Collider to never have your target blocked.

CameraController.cs
using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    [SerializeField] private CinemachineVirtualCamera virtualCamera;
    [SerializeField] private float cameraMoveSpeed = 5f;
    [SerializeField] private float rotationSpeed = 100f;
    [SerializeField] private float zoomSpeed = 10f;
    [SerializeField] private float minimumZoom = 1.0f;
    [SerializeField] private float maximumZoom = 50f;
    [SerializeField] private float selectedTargetMoveSpeed=3f;

    private CinemachineTransposer transposer;
    private float targetZoom = 7;
    private float currentZoom = 12;
    private Vector2 lastFrameMousePosition = new Vector2();
    private bool draggingScrollWheel = false;
    private bool isDraggingRotation = false;
    private Transform target;
    

    private void Awake()
    {
        transposer = virtualCamera.GetCinemachineComponent<CinemachineTransposer>();
        UnitActionSystem.OnSelectedUnitChanged += UnitActionSystem_OnSelectedUnitChanged;
    }

    private void UnitActionSystem_OnSelectedUnitChanged(object sender, Unit e)
    {
        target = e.transform;
    }

    void Update()
    {
        HandleMovement();

        HandleRotation();

        HandleZoom();
    }

    private void HandleZoom()
    {
        float scrollWheelInput = Input.mouseScrollDelta.y;
        if (!Mathf.Approximately(scrollWheelInput, 0f))
        {
            targetZoom = Mathf.Clamp(targetZoom - scrollWheelInput * zoomSpeed, minimumZoom, maximumZoom);
        }

        currentZoom = Mathf.Lerp(currentZoom, targetZoom, zoomSpeed * Time.deltaTime);
        transposer.m_FollowOffset = new Vector3(0, currentZoom, -10);
    }

    private void HandleRotation()
    {
        Vector3 rotationVector = new Vector3();
        if (Input.GetKey(KeyCode.Q))
        {
            rotationVector.y = 1f;
        }

        if (Input.GetKey(KeyCode.E))
        {
            rotationVector.y = -1f;
        }

        if (Input.GetMouseButtonDown(1))
        {
            isDraggingRotation = true;
            lastFrameMousePosition = Input.mousePosition;
        }
        if (Input.GetMouseButtonUp(1))
        {
            isDraggingRotation = false;
        }

        if (isDraggingRotation)
        {
            Vector2 currentMousePosition = Input.mousePosition;
            Vector2 delta = lastFrameMousePosition - currentMousePosition;
            if (!Mathf.Approximately(delta.magnitude, 0))
            {
                delta = delta.normalized;
                rotationVector.y-= delta.x + delta.y;
                lastFrameMousePosition = currentMousePosition;
            }
        }
        transform.eulerAngles += rotationVector * rotationSpeed * Time.deltaTime;
    }

    private void HandleMovement()
    {
        if (target)
        {
            LerpToTarget();
            return;
        }
        Vector3 inputMoveDirection = new Vector3();
        inputMoveDirection.x = Input.GetAxis("Horizontal");
        inputMoveDirection.z = Input.GetAxis("Vertical");
        if (Input.GetMouseButtonDown(2))
        {
            draggingScrollWheel = true;
            lastFrameMousePosition = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp(2)) draggingScrollWheel = false;
        if (draggingScrollWheel)
        {
            Vector2 currentFrameMousePosition = Input.mousePosition;
            Vector2 mouseDirection = (lastFrameMousePosition - currentFrameMousePosition).normalized;
            inputMoveDirection += new Vector3(mouseDirection.x, 0, mouseDirection.y);
            lastFrameMousePosition = currentFrameMousePosition;
        }

        transform.Translate(inputMoveDirection * cameraMoveSpeed * Time.deltaTime);
    }

    private void LerpToTarget()
    {
        transform.position = Vector3.Lerp(transform.position, target.transform.position,
            Time.deltaTime * selectedTargetMoveSpeed);
        if (Vector3.Distance(target.transform.position, transform.position)< .25f)
        {
            target = null;
        }
    }
}

Hah… yeah I was going to ask about camera zoom… I may have used a CoRoutine rather than lerp (which I assume is just a CoRoutine on its own anyway). I also use the new Input handler for it all and I am adjusting the rig radius for all 3 orbiters which is better than FOV for Cinemachine cameras. I am actually quite happy with the way it turned out.

I am just confused why you have this camera controller in the first place. The whole point of using the CM FreeLook was to not right this code. I assume that means the target group camera just won’t let you adjust it’s rotation or tilt at all without another script on top of it.

Actually, I think I was overtired last night.
This was my camera controller from the Turn based strategy course. You’re quite right, in the Third Person course, the Freelook camera covers things.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms