Make a Third Person Camera and Movement Controller in 7 Minutes | Unity Tutorial (or Download)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this week I wanted to have a look at how to create a third-person camera controller this covers character movement and camera movement around that character that's not waste time describing this though and get right into the tutorial first thing you want to do is create a new project and set up our scene we need the ground and the player character and of course we already have our camera use a cube to make the ground and scale this up then create a capsule for the player making sure it's centered and scaled correctly you can add materials to both the ground and the player if you like right click in the assets folder and create a material you can then select whatever color you like for those this next part you will want to right click on the player and add a rigidbody then set the constraints to freeze the X and Z rotations. next up create another cube and resize it this will be used to indicate what direction the player is facing. attach it as a child component to the player and edit its transform so it pokes out the front of the player the next part is very important too this will be used to control our camera and rotate around the player so right click and create an empty game object attach it to the player and reset the transform then alter the transform to match that's shown here finally attach the camera to the target as a child alter the transform too and match that which is shown on-screen now we have the scene setup I can create some code to control the game so create two scripts one called third-person camera controller and the all are called third person character controller that's the camera controller to the camera and the character controller to the player and then we can write some scripts starting off open the character controller to edit the first line is a public float speed which will represent how fast the player moves we also need to add a method called player movement that we will call insight update write this by adding void player movements and your brackets as seen here then we need our movement input for horizontal and vertical we get these using input.getaxis and look for hard and vertical and assign the values to floating-point numbers we then create a vector3 called player movement and this is equal to new vector3 hor (horizontal) comma zero f comma vert (vertical) and we multiply this by speed and time.deltaTime we use time dot delta team so that we can ensure the character moves smoothly and at the same pace across various hardware and it's independent of framerate then we actually move the player batting transform about translate their movement comma spaced out self make sure to save the script and go back to unity to try the site before playing though make sure you add a value for the speed that you want to move out so try some things like and see what feels right for your character next up we need to write the script for the camera controller so go ahead and open this up in the editor tidy up the script a bit and add in our initial variables these are the rotation speed which i am setting to warm but you can mess around with this to see what works for you then we create our to public transforms for target and player and of course our floats for moist eggs and mouse why we need to create a method that will control and move the camera called cam control and we want to we want this to be called inside blade update so similar to the movement script we did previously we want to get our user input first and assign it to fluked values so add noise x plus equals input.getaxis mosaics multiply it by rotation speed and then Moi's Y minus equals input.getaxis most way names rotation speed and moist ye will control the camera moving up and down so we want to make this negative but some people might prefer to be flipped we also want to clump the most on the wave rotation and we do this with most y equals math f-duct clump bracket Moi's y comma minus 45 comma 60 close brackets and this prevents the camera from flipping around so it doesn't get too high and go fully over under player next I'd transformed outlook that target to get the camera focused on the right area and to control the rotation ad target dot rotation equals quaternion dot order moist white mosaic zero and player dot rotation equals quaternion boiler zero mosaics zero and this allows the camera to rotate but also for the player to rotate teeth so back in the start method you can also add cursor dot visible equals false and cursor dot block state equals cursor lock mode locked this removes the cursor and locks it to the center of the screen so it won't be in the way when you're playing and then save everything and go back to unity click on the camera in the hierarchy and there will not be two slots available in the inspector for our script and this is for the target and the player so dragged off our target and our player into these relevant positions you can also change the rotation speed here once that is complete try playing the game and see how it works you may see more clearly what is happening if you look at it from the scene view while you're playing now that is essentially how you would create a very basic third person character and camera controller you can even add in a little boomers feature by doing the following to the cam control method so if you go back to the camera controller script a not in the following if input dot get key key code left shift then we only want to rotate the camera else we rotate both the camera and player like we can see here so save the script and go back to unity and if you try this side you cannot rotate the camera by itself if you hold on the left shift key you can move through the level and rotate the camera around the player while doing this another issue that you may have with this camera is whenever you have a wall or object in your game so if you look at this wall when you move back against it the camera goes through the wall and you can no longer see the player so in the next video I will show you how to hide the walls so that the camera can see three through the player like it is shown here and with that say I'd be a finished in all our tutorial but make sure to subscribe for more videos coming soon and I'll see you in the next one
Info
Channel: Stephen Barr
Views: 206,972
Rating: undefined out of 5
Keywords: Unity Tutorial, Unity Engine, Unity3D, Game Development, GameDev, Education, Learning, C#, Third Person Game, Third Person Camera, Camera, Movement, Tutorial
Id: 7nxpDwnU0uU
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Wed Sep 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.