Want a Rotating Camera? Do this

@Rick_Davidson
@sampattuzzi
@Brian_Trotter

So I said when I implemented a rotating camera, I would come back and post it here…

Go to Cinemachine and add a free look camera.

Screen Shot 2020-05-23 at 10.24.30 AM

Add the free look camera to your Core prefab (or camera prefab if you have one, here you can see I disabled my old follow camera from this lecture and now use the free look)

Screen Shot 2020-05-23 at 10.25.28 AM

Make sure your fee look camera has a higher priority than anything else… like maybe 100 or something, and set your follow and look at to the Player

Screen Shot 2020-05-23 at 10.28.34 AM

…and make sure to tag the camera “PlayerFramingCamera” for the below scripts I will share.

Screen Shot 2020-05-23 at 12.51.44 PM

I have suggested settings below… to learn more about what these do watch this vid

I suggest these settings, invert x an y axes as desired…

Screen Shot 2020-05-23 at 12.39.21 PM

open the rig settings and scroll down to the bottom rig and apply this suggested setting

Screen Shot 2020-05-23 at 12.41.58 PM

finally put the following script on your player prefab…

CameraController.cs
using UnityEngine;
using Cinemachine;
using RPG.Control;

namespace RPG.Core
{
    public class CameraController : MonoBehaviour
    {
        [SerializeField] GameObject freeLookCamera;
        CinemachineFreeLook donkey;
        PlayerController playerControllerScript;

        private void Awake()
        {
            donkey = freeLookCamera.GetComponent<CinemachineFreeLook>();
            playerControllerScript = GetComponent<PlayerController>();
        }

        private void Update()
        {
            if (Input.GetMouseButtonDown(1))
            {
                if (playerControllerScript.isDraggingUI) return;
                // use the following line for mouse control of zoom instead of mouse wheel
                // be sure to change Input Axis Name on the Y axis to Mouse Y
                //donkey.m_YAxis.m_MaxSpeed = 10;
                donkey.m_XAxis.m_MaxSpeed = 500;
            }
            if (Input.GetMouseButtonUp(1))
            {
                // use the following line for mouse control of zoom instead of mouse wheel
                // be sure to change Input Axis Name on the Y axis to Mouse Y
                //donkey.m_YAxis.m_MaxSpeed = 0;
                donkey.m_XAxis.m_MaxSpeed = 0;
            }

            // wheel zoom //
            // comment out if using mouse control for  zoom
            if (Input.mouseScrollDelta.y != 0)
            {
                donkey.m_YAxis.m_MaxSpeed = 10;
            }
        }
    }
}

and drag the free look camera into the script

Two things about adding this…

  1. This setup is to retain left mouse for actions / right mouse to rotate camera… if you want to change this, these are the scripts you will need to edit… replacing GetMouseButtonDown(0) (be sure to replace the mouse button in the camera controller accordingly)

  1. That last script CameraFacing will break, so replace the code with this…
CameraFacing.cs
using UnityEngine;
using Cinemachine;

namespace RPG.Core
{
    public class CameraFacing : MonoBehaviour
    {
        [SerializeField] CinemachineFreeLook playerFramingCamera;

        private void Start()
        {
            playerFramingCamera = GameObject.FindGameObjectWithTag("PlayerFramingCamera").GetComponent<CinemachineFreeLook>();
        }

        void LateUpdate()
        {
            transform.LookAt(2 * transform.position - playerFramingCamera.transform.position);
        }
    }
}

and BOOM… You’re a Rock Star. Or at least your player will be… you can zoom RIGHT DOWN in there and admire all your lovely art and see far off in the distance, or scroll up and get a grand bird’s eye view of ALL the action and lots of environment…

Have fun tweaking this.

15 Likes

Wow, you really put some work into this! I’m impressed. Well done. @Lucy_Becker you might want to talk to @HeathClose about turning this into a blog post, as a rotating camera in the RPG is something students ask about all the time.

3 Likes

This is gold, bump this for everyone to see. Thanks Heath.

1 Like

This is awesome @HeathClose, thanks so much for taking the time to share.

As Brian suggested, how do you feel about turning this into a blog post for us? That way we can share with the even wider game dev community :slight_smile:

1 Like

So using Cinemachine while playing (not in Cutscenes) is something that “should be done”?

I thought that Cinemachine is only useful for Cutscenes etc.

Cinemachine can be used in any way that works for you. I find it very easy to implement and use it just about every project.

1 Like

Sure, I’d love to!

1 Like

Thx. Do you know a good (and possibly complete) tutorial about cinemachine?

unity learn is free right now… I would say try that

Great tutorial man! Thank you :slight_smile:

Have been trying to get this working for a bit, thank you for posting this tying up my loose ends!

1 Like

Privacy & Terms