banner



How To Rotate Camera In Unity

Rotating and Zooming Camera in an RPG using Unity's Cinemachine

One of the about requested features students inquire for while taking the RPG course is how to implement a rotating photographic camera.  While going through the class myself, once we reached the lecture for "Creating a Cinemachine Follow Camera", the resulting camera was great, but sometimes I wanted to zoom right downwards on my character and appreciate the art from GameDev.tv partner Synty and the code systems I learned to build.  I immediately implemented a zooming script and posted it for other students.  Knowing I also wanted to be able to rotate the camera to view the action from any angle, I mentioned that I would circumvolve back to that same lecture and postal service how to create a rotating camera as soon equally I figured out how to do it.

Here is how I did it.


  1. Open up the Cinemachine menu in Unity and create a gratis await camera.
  1. Add the free await camera to your Core prefab (or camera prefab if you lot take i, here you can see I disabled my old follow photographic camera from that lecture and now utilize the gratis look)
  1. Make sure your fee look camera has a higher priority than anything else, like maybe 100 or something obvious, and assign your Player prefab to the "follow" and "look at". (Remeber to override this priority with your cinematics if you take any.)
  1. I will be sharing some code below that will look for this camera past information technology'due south tag. Yous tin change the tag on the camera and in the lawmaking to anything you want, only if yous want to but copy and paste the lawmaking, make sure to tag the photographic camera "PlayerFramingCamera" as that's what I used in the below scripts I will share.

I'm going to share some settings to get you started with setting upward the free look camera.  For those of you who want to learn more about what these settings do, refer to Unity's video on the gratuitous expect camera.

  1. In the Axis Control section of the free wait camera, match your settings to the suggestions in the image below, inverting the X and Y axes to adapt your play style. (Claiming: Allow the user to change the 10 and Y invert options in game in a settings menu!)
  1. Open the rig settings and scroll down to the lesser rig and utilize this suggested setting.
  1. Now it'due south time to attach the following script to your Actor Prefab. The proper noun must match the class for Unity to compile correctly so name the script CameraController. (This script is in the namespace RPG.Core, only you lot tin alter this or delete this to suit your needs, just be aware of information technology and how it applies to your code's architecture.)
              using UnityEngine; using Cinemachine; using RPG.Command;  namespace RPG.Core {     public class CameraController : MonoBehaviour     {         [SerializeField] GameObject freeLookCamera;         CinemachineFreeLook freeLookComponent;         PlayerController playerControllerScript;          private void Awake()         {             freeLookComponent = freeLookCamera.GetComponent<CinemachineFreeLook>();             playerControllerScript = GetComponent<PlayerController>();         }          individual void Update()         {             if (Input.GetMouseButtonDown(ane))             {                 if (playerControllerScript.isDraggingUI) render;                  // use the following line for mouse command of zoom instead of mouse wheel                 // exist sure to alter Input Axis Proper noun on the Y axis to "Mouse Y"                  //freeLookComponent.m_YAxis.m_MaxSpeed = 10;                 freeLookComponent.m_XAxis.m_MaxSpeed = 500;             }             if (Input.GetMouseButtonUp(1))             {                 // use the following line for mouse command of zoom instead of mouse bicycle                 // be sure to alter Input Axis Name on the Y axis from to "Mouse Y"                  //freeLookComponent.m_YAxis.m_MaxSpeed = 0;                 freeLookComponent.m_XAxis.m_MaxSpeed = 0;             }              // wheel zoom //             // comment out the below if condition if you are using mouse control for zoom             if (Input.mouseScrollDelta.y != 0)             {                 freeLookComponent.m_YAxis.m_MaxSpeed = ten;             }         }     } }            

This script will detect whether or non yous are holding downwards the right mouse button.  It then gives you access to dragging and moving the camera on the 10 axis.  Zooming with the mouse curlicue bike is baked into the gratis look component, but you tin can also use the Y centrality of the mouse to zoom in just by uncommenting the Y axis line and making certain you alter the Input Axis Proper noun in the free expect component to "Mouse Y" where you see "Mouse ScrollWheel".

  1. At present that the CameraController script is attached to your Role player Prefab, exist sure to drag the free look camera into the Serialized Field for the camera game object variable.
  1. There are ii things to keep in listen when calculation this rotating camera to an already existing RPG adult from the RPG grade.
  • This setup is to retain left mouse for actions / right mouse to rotate camera. If you want to alter this, these are the scripts yous will demand to edit, replacing GetMouseButtonDown(0) with the mouse button of your choice.
Scripts to change mouse buttons in...

Be sure to change the mouse push in the CameraController script accordingly.

  • The script we adult to get enemy health confined to face the photographic camera at all times will pause, namely CameraFacing (the last tab in the film above). Yous tin come across the issue by playing the game right now. A dandy eye will notice that the free expect camera never rotates... on a rotating camera...

To fix this, supplant the code on the CameraFacing script with this (as well keeping in mind the namespace).  This is the code you tin either change the tag it looks for or make certain you tag it "PlayerFramingCamera" equally discussed higher up.

              using UnityEngine; using Cinemachine;  namespace RPG.Core {     public class CameraFacing : MonoBehaviour     {         [SerializeField] CinemachineFreeLook playerFramingCamera;          private void Starting time()         {             playerFramingCamera = GameObject.FindGameObjectWithTag("PlayerFramingCamera").GetComponent<CinemachineFreeLook>();         }          void LateUpdate()         {             transform.LookAt(2 * transform.position - playerFramingCamera.transform.position);         }     } }            

Nail… You're a Rock Star. Or at least your role player will be.

Yous can zoom RIGHT DOWN in there and admire all your lovely art (or Synty'south) and see far off in the distance at the aforementioned time, almost similar a 3rd person shooter's perspective.  Or curl up and get a Thou bird'due south heart view of all the action and lots of surround.

With endless permutations, the end issue will exist upwardly to you or your designer.

Accept fun tweaking this!

Source: https://blog.gamedev.tv/rotatingcamera/

Posted by: moorerthund1988.blogspot.com

0 Response to "How To Rotate Camera In Unity"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel