Move Character to Click Location with Unity Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi and welcome to another unity tutorial in this tutorial I want to show you how you can move a character around on a terrain by clicking on the terrain and have the character turned towards that point and then move to it this is a terrain but this will also work on anything that has a Collider on it for clicking and setting up the position a little later I'll also show you how to add this projection that sits around the character and will use it to define the limits to which the character can move within for each turn to begin you're going to need a terrain and I've just created a really quick terrain here and you'll also need a character and in this case I've got the tank from the unity tanks tutorial that you can find in the asset store if you want to follow along when you don't have this tank you can also use a cube it's going to work the same way on your character in this case the tank you want to add a rigidbody and also a box Collider so select the tank object and go add component and you want to look for rigidbody and also the Box Collider when you add the box Collider onto your object you might need to resize it I just zoom in here you'll be able to see the green lines are the Box Collider now if you add a box Collider onto this particular tank it will be smaller than the tank and you'll need to resize it to do that easily you can click on this button over in the inspector next to edit Collider and it will give you some little dots on the side of that Collider and if you select those dots you can modify the collider in real time in the scene view so you want to set that clutter up around your object as neatly as you possibly can also make sure that the collider is above the terrain or the ground plane that you're using otherwise when you press play it might fall straight through next to create a new C sharp file and call it tank controller case I've already got one but it's in the project go create c-sharp and call it ten controller when we open that code up you'll see that it's pretty much empty the first thing we have to register is the mouse clicking in the window and to do that we'll put the code inside of the update function and it's going to look like this so we're saying if the left mouse button is being held down that's the get mouse button function and it's zero for the left mouse button and it would be one if you wanted to use the right mouse button so this will register a mouse click and then when it does we want to run something called set target position and it'll be our own method that we write to grab hold of the position where the mouse is clicking on the terrain and then using that to set a target location for the tank to set target position function or put down here and if it'll look like this now what this function is doing is its casting array into the environment from the position of the mouse so input Mouse position will give us a position of the mouse on the screen and then this camera dot main screen point array will turn the mouse position into array heading off into our environment conceptually what we're doing is we're taking the mouse position and we're pushing a line directly out from it which is perpendicular to the screen and looking for where that line hits the environment that line is our Ray so it's a vector from the screen into the environment and where it hits the collider of the terrain that's the hit point back in our code the Ray is declared here and the hit point is also declared here but it isn't a given of value what gives it its value is this physics raycast which sends the Ray into the environment and then brings back our hit position now that ray that was that means the environment is set to be a certain distance because of course you could send a ray out into the environment and it could go on infinitely and you don't want it to go that far in this case I've set it to a thousand if you're doing this and you don't seem to be hitting anything try increasing this value here as well to make sure that you have extended your Ray long enough into the environment to hit something now just before I test this also I've got the target position that I'm setting with the hit dot point value that we're getting back from the terrain hit and that's because I'm going to use this target position a little later this target position is a vector three so at the top of our program we want to declare that as a vector three target position like that and then just save that code let's go back into unity make sure that you attach your tank controller code to your tank or your cube whatever you're using and check in the inspector that it's actually attached down here right so now when we run this we can click on the environment in the game view and you'll see that wherever you click the tank now turns to face in that direction and I can also hold the mouse down and it will do that as well now that's because back in this code we're using the DES transform look at function the look at function will turn an object so that it's z-axis is facing the position that you give it to face so as long as the forward direction of your model be fine tank in the scene is along the forward direction which is your z-axis the blue one here you'll be right so your model will be facing in the right direction now we don't want the tank to be snapping around looking at each click position instantaneously you'll want the tank to turn on the spot and appear to be turning so we're not going to use this look at here though you might want to use it in other situations I'm just going to comment that out for now what we need to do is to calculate the angle towards the new position or the hit point I should say now to do that I'm just going to add in some code below the calculation for the hit point and that is a look at target vector and this will be the vector which is this will be the vector pointing in the direction of the Ray so essentially is the same as the Ray except I'm giving it the same y-value as the tank itself this will keep the tank turning on the flat so if you've got an object that needs to move along the ground you don't want it to end up looking up in the air by complete accident then you want to turn it around its y-axis which is it up axis and you only want to turn it in the X and the Z direction so left and right essentially so by finding only the left and right differences then we've got a position that will rotate the tank so if we just go back to unity for a second to show you here so this is our tank let's say that I click up on top of this mountain over here if we were to look at that mountain from where the tank is the tank would turn and let's see if I can just do this for you in an instant it would turn like that and it would be looking up at that position which is quite an unnatural position for the tank now because we've got the physics on it it tends to just drop back down for us instead what we want to do by keeping that y-value the same as the tank is that if the tanks like facing that direction and I click up on the mountain here it will calculate the rotation that we need to do like that so directly around its up axis where it's going to take this point and project it down so that it's are the same height as the tank and that will stop anything from sort of looking up in the air or rotating when you click on a mountain this would be a problem if you have an animated character who's sort of humanoid and you want them to run up a hill it could also rotate your character unfortunately so that it's not standing upright on the ground so this is how we remove that by removing any problem with the y-axis all right so that's our look at target and then the next thing we're calculating is the rotation that the player has to take so this is a tank's rotation that has to occur in order to look at the target that we're calculating here so this look rotation works out the angle you need to turn to be looking at that particular target now this look at target is a vector3 that we need to define at the top and this player raat is a catonian which we are also going to define at the top so let's go up here and add in our vector three and our catonian which is our structure that holds the rotation okay so that's in there right now that we've done this we can write a function called move which will move the tank and perform the turning back in the code just below the set target I'll create a new function called move and what move looks like is in the rotation of the tank and performing a slurp for it and a slurp allows you to transition smoothly between one rotation and the other so this is the rotation that the tanks currently facing and this is their rotation the player rotation we calculated up here where we want it to be facing and so we'll get a smooth movement between these using a speed and in this case it's rock speed which we need to define rotation speed multiplied by our time Delta time to smooth it off so up the top of our program we will now define raat speed which will be a float and I'm going to set that to five you can modify this if you want the tank to turn quicker and now we can run our move function which I'll just put inside the update so the updates going to go if mouse buttons down set a new target but we'll always be running our move function to turn the tank around so let's save that and we'll go back to unity and try it out so if I press play you can see now that when I click the tank flips to the position rather than snapping the next step is to get the tank moving so let's go back to our code and we'll make the tank move in the move function because that's what it's for so we're going to rotate towards it and then after that we'll add in another line which is doing a move so the move towards function in this case is taking our position that the tank is in and our target position that we calculated earlier which was up here and it's the hit position on the terrain and we're applying that to the position of the tank so it's going to push the tank along towards the target position at a speed which is defined here so we need to create this variable speed at the top and I'll put that without rotation speeds it's also going to be a flow speed and let's set that to 10 and again you can play around these values so I'll just save that go back into unity and press play all we can see is the tank immediately slides off the map what it's doing is moving towards our target position which it has not been set yet because we haven't clicked if you click in the environment the tank will eventually come back from wherever it's gone to let's just start again I'll let it slide away and then click and then it will go to the new position the other thing that you can see is that when it gets to that new position is jittering and that's because the move function is running every update and we don't want it to run every update and we also don't want it to run right at the beginning so let's add a little bit of logic in there to say if we have something to move towards move towards it and if you already at the location that you need to be at then stop moving so the first thing we'll add at the top ball moving and we'll set it to false initially because we're not moving anywhere when the co starts to run next inside the update around the move we'll put F moving then run the move function so it's not until this moving becomes true that this here will run now when it becomes true well happened down in the set target position after we've created our hit point and so let's put moving equals true into here so that'll set it to true which will make the move function up in the update run but we also want to stop moving when we reach our destination and that part of the code will go inside the move so just down here let's add that code in and that says if our transform dot position so the position of our tank is equal to the target position then we can stop moving if you don't want to be this accurate then you could add within a particular range of your target position because sometimes depending on how you're moving your object around it might never get to an exact position and therefore you might want to range in there instead in this case we're saying it is exactly on that spot because with the slur from the move towards we should manage to get it at the right location so let's save this and I'll just go back into unity and run and immediately when we press play you can see the tanks not going anywhere because moving is currently set to false so if I want to move over here I can click the tank will turn around and head over in that direction you can also see that the jitteriness is stopped because that move function isn't trying to move the tank when it's on its destination so it's not even running it will be set to false in this particular case because of the way what this physics set up if I try and go up this hill it's more than likely that my tank might decide to roll back and down and it is as you can see there because it's stopped and it's not stuck on that terrain because the physics is still going to influence that tank and you know you could restrict the physics on there so that the tank cannot roll around in certain axes and you do that by selecting the tank and going to the rigidbody and having a look at the constraints if you only want the tank ever to be able to rotate around its up axis then you can freeze the rotation on the Z and the X and that will stop it from tumbling around unexpectedly and I'll leave this tutorial here so you now have a nice tank that moves around to the quick location and it moves smoothly and it turns smoothly as well in the next video I'm going to make I'll show you how to project a target location around the tank onto the terrain and then to restrict the movement of the tank within that range so for example you're making a turn-based game like a strategy game and you only want units to be able to walk so far or move so far each turn then I'll show you how that is possible thanks for watching and as always if you'd like to support the development of more tutorials and you'd also like to request tutorials then come along and join up on the patreon website all Patriots who subscribe will get access to my you to me courses and I also get access to all of the project files that I put up on YouTube
Info
Channel: Holistic3d
Views: 36,114
Rating: undefined out of 5
Keywords: Unity 5, unity, tutorial, c#, beginner, move, character, click on terrain, terrain, move to click, automate, animation, navigate, navigation
Id: MAbei7eMlXg
Channel Id: undefined
Length: 18min 16sec (1096 seconds)
Published: Tue Jun 06 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.