Creating a Third Person Camera using Cinemachine in Unity! (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
cinemachine is a great tool in unity that provides you with a set of features to assist in building functional cameras for your games and animations and works in both 2d and 3d cinemachine is designed to help ensure that your camera operates intelligently and captures exactly what you need when you need it traditionally it can be quite difficult to build certain types of camera systems such as a third-person camera which often relies on smart framing and controls so in this video we're going to take a look at how we can set up a third person camera using the new aiming rig of cinemachine 2.6 and how we can use impulse propagation and blending to create additional gameplay functionality for a camera controller to get started using cinemachine in your project you'll first need to install it from the package manager note that if you're upgrading from the asset store version of cinemachine you'll need to delete the cinemachine folder from your project's assets folder before installing this version from the package manager now that cinemachine is installed we're ready to start setting up our camera in this scene i have a playable character who can be controlled with the keyboard or gamepad i can also aim and fire the bow and shoot projectile arrows currently the camera is pretty static my character will move relative to their forward direction but it can get quite tricky to control them from one angle you might consider simply attaching the camera directly to the player's avatar but this results in a rigid uninteresting view of the game and for many players this approach causes motion sickness this is where cinemachine is useful because it makes it much easier and faster to set up a more responsive third person camera let's create a new virtual camera by choosing cinemachine create virtual camera notice how this camera is created at the exact position of our scene view this is one of the new features in cinema machine 2.6 making it much easier to frame and create a shot if you are creating a static camera this feature is an extremely useful way to position your cameras and block your shots for this demo however we're going to be creating a camera that positions itself procedurally in the body of our cinemachine virtual camera let's change the body property from transposer to third-person follow next since we're making an over-the-shoulder camera we need to designate a target as our player's shoulders we have created a child game object called follow target which is positioned at the neck height of our player for cinemachine to focus on let's assign this to the follow property of our cinemachine camera our cinemachine camera will automatically move to and position itself relative to our target's position and forward direction we can use the camera side slider to offset the position of our camera to either shoulder we can also easily adjust the camera distance and field of view to better position the camera additionally we can adjust the z dampening on the third person follow settings to have the camera chase behind the player whenever they move with just a few steps we already have a much more responsive camera and a lot more flexibility to create the shots we want there are a few more simple things we can do though to make this camera feel even better at the moment the character's rotation is tied directly to the input however what if we wanted to rotate the camera around the player instead as cinemachine is tracking our followed target transform we can rotate the follow target game object instead of our character and our camera will follow the rotation of our target and rotate around the character we created a script that takes horizontal and vertical input and then rotates our follow target which will effectively rotate the camera around the player we can also extend this further to rotate our follow target based on the vertical input axis of our controller so that our camera can also look up and down here we check our current x rotation of our follow target and clamp it so that our camera is locked to reasonable vertical rotation then whenever we're moving we just set the y rotation of the player to the local rotation of the follow target transform and then reset the local y rotation of the follow target transform now we have a functional and rotating person camera controller that allows us to look around while moving and rotate around our player whenever they're idle this is feeling pretty good however there's still a bit more we can do to improve the feeling of our camera our player can draw their bow and fire arrows at things in the scene but it feels a little underwhelming one way to improve this is by having our camera respond to our player firing the arrows through impulse propagation cinemachine's impulse listener allows for objects such as our camera to listen and react to impulse events in our scene when an impulse is received by the listener it will respond according to the noise settings in the impulse event we want our camera to react to our arrow as it's being fired so first let's add an impulse listener extension to our virtual camera next let's select the object we wish to generate our impulse event from in this case i'd like the arrow to trigger an impulse as soon as it fires so let's select the arrow projectile prefab in our project and add a cinemachine impulse source component an impulse source is a component that emits a vibration signal from a point in our scene the impulse channel setting allows us to filter different impulse events and listeners this can be useful if you only want certain objects listening and reacting to specific impulse events from objects in your scene the signal shape settings control the basic characteristics of the vibration when emitted from our impulse source let's also create a new raw signal asset for our arrows impulse i want the arrow impulse to shake the camera vertically and horizontally slightly so i've drawn out two curves here on the x and y axis these parameters are currently defined by world space coordinates and so we will need to convert them to camera space later so that our camera shake is consistent regardless of the direction it's facing we can set the amplitude and frequency of the impulse event from this component and adjust the spatial range settings of our impulse blast the amplitude gain property affects the strength of an impulse source's vibrations emitted by the raw signal the shape of the signal is preserved but it is amplified or attenuated according to the gain setting the frequency gain property affects the timing of the raw signal and controls the speed of the vibrations think of it as a way to speed up or slow down time so that the signal plays out in slow or fast motion the time envelope property controls the duration of the impulse and the intensity of the raw signal over that duration it creates an overall shape into which the signal is fitted this is a way to control the ease in and ease out of the noise signal the repeat mode specifies whether to loop the raw signal over the duration of the time envelope or stretch the raw signal to fit for this example we can leave these parameters at their default settings the spatial range settings determine the area of effect for the impulse signal to move through any listeners within the spatial range will be affected by the impulse when it is invoked the impact radius determines the space in the scene around the impulse source where the signal remains at full amplitude beyond that radius the signal strength then fades out or falls off relative to the dissipation distance this allows listeners that are farther away from the source to react less than those that are closer so it's worth keeping in mind that the total effect of the signal combines both the impact radius and dissipation distance we can also set the dissipation mode to define the aggressiveness of the fall off of our impulse source our arrow is going to be pretty close to our camera so we shouldn't need to adjust these much from their defaults finally the propagation speed will control how fast our impulse event passes through the impact radius set the speed lower if you wish for the event to move more gradually through the area of impact causing listeners that are farther away from the source to react later we want our impulse to move pretty quickly and by default this is set to be the speed of sound which is 343 meters per second for more information on impulses and impulse sources in cinemachine follow the link in the description below all we need to do now is tell our impulse source to generate whenever our arrow is fired we can do this by creating a reference to the impulse source script and calling the generate impulse method we'll also pass in the camera's forward direction to the generate impulse method so that the impulse signal is rotated from the world space coordinates to camera space coordinates as we discussed earlier now our camera reacts and shakes any time our arrow is fired by the player we can adjust the gain in the listener to amplify or reduce the effect of the noise to our taste notice here that when our player fires the position of the center screen reticle doesn't always line up well with where the character is actually aiming this is due to the slight difference in the perspective of the camera behind the character and the perspective from where the character is aiming you can especially see this parallax effect when the character is aiming at something close to it we can adjust the reticle to be aligned with the character's view using the third person aim component this component casts array from the follow target's position along its forward direction and places a secondary reticle over the ray's intersection with the first object in its path that reticle indicates what would be hit if a projectile were to be fired from that spot in that direction one final feature that can really add to the feeling of a camera system is adjusting the camera settings when the player performs different actions we can do this by toggling different virtual cameras and blend between different camera states for instance when a player aims we can toggle to a second cinema machine camera with similar settings but a tighter fov and a right aligned camera i'll show you how we did this to start let's duplicate our camera and adjust its settings to better suit aiming for our aim camera we'll zoom in by decreasing the field of view and decreasing the distance to the player then let's create a script to switch between our cameras based on if the player is aiming or not we can do this by checking the value of our input and then activating the corresponding camera so when we press the button to aim we toggle the aiming camera on and the main camera off when we let go we toggle them back let's also adjust the default blend time from 2 seconds to 0.5 seconds so that the transition between the two states is faster now whenever our character enters the aim state our cameras toggle and cinemachine will blend between them as you can see cinemachine and the third person follow rig allows us to create a much more dynamic and more responsive third person camera system with ease for more information on using cinemachine with unity and to download this project and to try the third person camera features yourself follow the links in the description down below thanks for watching
Info
Channel: Unity
Views: 437,760
Rating: undefined out of 5
Keywords: Unity3d, Unity, Unity Technologies, Games, Game Development, Game Dev, Game Engine, unity3d, unity technologies, unity tutorial, unity game development, game dev, 3rd person camera, third person camera, third person camera cinemachine, cinemachine, unity cinemachine, how to cinemachine, how to unity, camera controller unity, third person controller unity, aiming rig cinemachine
Id: 537B1kJp9YQ
Channel Id: undefined
Length: 11min 39sec (699 seconds)
Published: Mon Sep 21 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.