How to Move Characters in Unity 3D: Built-In Character Controller Explained [#1]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we want to get our characters moving using unity's built-in character controller but we have no idea how it all works not a problem today we are going to break down and apply the methods and properties available to us with unity's built-in character controller that way we can make any modifications necessary so that movement feels just right in our own games welcome to iheartgamedev my channel all about game development if you enjoyed today's video please consider liking and subscribing and now let's get started for this project we have a small environment equipped for testing the built-in character controller in the inspector we'll see a parent character game object which will host the built-in character controller component and a movement script the child of the character game object is the downloaded shamo character model featured in the hit youtube channel mix and jam okay when we add the built-in character controller component directly using add component and searching for character controller the game object will gain access to a list of methods and properties the most immediate and visible difference when first adding the character controller is the capsule collider included with the component using center radius and height we can properly align the collider with our character model of the child component so that it interacts with colliders as we would expect it to typically we align the collider at the center point of the character followed by adjusting the height so that the collider reaches both the feet and head and the radius to fit comfortably around the character's body character controllers are designed to provide collision detection and movement to our characters it's important to understand that this parent character game object is in control of these two things even if we disable jammo the capsule collider still moves and still collides jmo is merely cosmetic it's also worth noting that even through code we cannot rotate the built-in character controller's collider it will always stay aligned on the world y axis this is definitely something to take into consideration when planning your games and deciding if the built-in character controller is the right fit or if it makes more sense to use a kinematic or dynamic rigidbody instead with a separate collider which is a completely different type of character controller that we'll cover in the future the min move distance property sets the smallest change in distance required for our character controller to actually perform the move while recommended to typically have this property set to zero it's worth noting that the property exists to begin with to remove potential unwanted jitter for instance if our movement code includes some form of deceleration that for whatever reason doesn't always reset its speed to an exact value of zero then we can set a small min move distance to prevent the character from continuing to move despite the speed not being exactly at zero it's worth noting that this number should remain incredibly small min move distance is evaluating the change in distance between the current position and its next position after the movement method would be applied this distance is typically a small fraction added every frame because our movement logic is programmed to be frame rate independent the skin width property is designed to prevent characters from getting stuck inside of other colliders think of it as the first layer of defense for a head-on collision when the collision is directly perpendicular to the direction that the controller is moving the skin width will stop the character controller from continuing to move in that given direction at an angle the character controller skin will allow some penetration but will still prevent the controller from fully reaching the original collider and when the incoming collider runs parallel to the direction that the character controller is moving the skin width is ignored research indicates that this is intended to prevent the character controller from penetrating and getting stuck inside of other colliders providing a personal space between the character controller and the environment or other characters it's recommended in the docs that the skin with is around 10 percent of the value given to the radius property the step offset value equates to the height of an incoming game object or more specifically the height of its collider that the move or simply move character controller methods will automatically mount and climb when the collision is detected this is measured from the bottom of the character controller's capsule collider to the height of the collision points collider where the collision occurs if the height of the collider is greater than the step offset the character controller will not step up and as we can imagine the name step offset indicates this property's usefulness for handling movement of steps the final inspector property is the first on the list the character controller's slope limit this value dictates the degree angle that our character controller will climb anything with a higher angle and the character controller will stop completely and of course anything lower and the character controller will be able to move up and down before we move on to learning about the two methods move and simple move there's still a few properties accessible to the built-in character controller that we can find in unity docs detect collisions enable overlap recovery collision flags is grounded and velocity detect collisions will enable and disable collision detection from incoming rigidbodies and will allow the character controller's collider to avoid impacting rigid bodies altogether it handles each of these situations differently if detect collisions is disabled and a rigid body collides with our moving character the character will just step over it similar to step offset however if the character is stationary meaning that neither of the two move methods are actively being called the rigid body will just pass through when would we actually use this the main use case would be scripted events in our games if a game says press x to enter a car typically there's a scripted animation that will have the character enter the car which has its own set of colliders we'll notice that the animation moves jmo but not the character controller so this would be the perfect instance to disable collisions reposition the center of the character controller's collider to realign with jammo and avoid impacting the car's rigid body enabled by default enable overlap recovery is used to de-penetrate the character controller from static game object colliders when an overlap is detected what this means is if our character were to somehow penetrate through a collider of an object that is quote static static meaning it does not move at runtime unity will automatically try to push our character controller out of the static object in the closest point available it's worth mentioning that without the character controller's move or simple move function being called the character controller will not be removed from the collider but once the movement is invoked they will immediately be removed collision flags are points on the character controller's collider that tell us where the collision is occurring there are collision flags on the bottom middle and top of the character controller's collider allowing us to see which part of the character controller is included in the collision for instance the bottom collision flag titled below is true when the character controller is touching the ground and false when it's not and it just so happens that the is grounded property is set to the below collision flag however collision flags function a little differently than expected collisions are detected based on the last invocation of the move or simple move functions what this means is that even if the character controller is touching another collider if the controller's movement is not in the direction of the collider then the collision flag will not be true for example even if the character controller's transform y value is at zero and so is the ground collider if the vertical velocity is set to zero then the character controller is technically hovering and is grounded will be false understanding this will be really helpful when programming a proper jump implementation another main concern with collision flags is that they aren't the most specific we don't know if the collision is from the side the back or the front of the character controller a nice to have feature or improvement would be to have these collision flags be a little more detailed possibly providing the direction of a given collision relative to where the character controller is currently facing lastly we have velocity which will simply tell us the current velocity of our character controller but more specifically the velocity created from the move and simple move methods it will not take into account any added movement from outside sources say if we are on a moving platform it's specifically the character controller's movement and that covers all the proprietary properties of the built-in character controller [Music] let's learn about the two movement methods move and simplemove are both methods that accept vector3s as their arguments a vector3 is basically a coordinate point in the x y and z axis move and simple move take the current position of our character controller which is its own vector 3 and add this new vector3 value this change is what moves our character controller in a given direction as shown here as we modify the vector3 that's being passed to the move method the character controller consistently moves on the x y and z axis and detects collisions depending on the direction it's moving we may find it helpful to understand the distinction between moving the character controller and the animations of the child character model it's relatively eye opening when playing renowned games like mario and zelda and then realizing that both characters are there for cosmetic purposes and that there's likely a capsule collider or something similar actually handling the movement now there are noteworthy differences between move and simplemove focusing on simplemove this method is framerate independent what this means is that behind the scenes the vector3 that we pass in as an argument is likely multiplied by time.deltatime without us having to do so ourselves time.deltatime is the time since the previous frame was drawn in other words a fraction of a second so when we multiply our vector3 by time.deltatime this typically makes the distance move per frame much smaller frame rate independence isn't something that's exclusive to simple move it's just something that's automatically done for us instead if we were to switch to the move method we would need to first multiply the vector3 by time.deltatime before passing it to the method otherwise our character would move at crazy and inconsistent speeds additionally simplemove completely disregards the change in the y-axis and applies gravity by default for this reason if we plan on having our character jump simplemove is just not the right choice move is a much more versatile method but requires more work we can get a jump working but we will also need to remember to add that time dot delta time multiplier to make it frame rate independent too for both it's also recommended that each method is only invoked once per update this is worth remembering because move and simple move impact other properties like the is grounded boolean and the collision flags so calling these methods more than once can lead to some unexpected and unwanted results the last method we'll cover today is the on controller collider hit callback method like many of the other properties this callback function is a direct result of the move and simplemove methods what this means is that when our character controller is moving and it hits another collider this method will be invoked and will grant direct access to the game object that is part of the collision however if an opposing game object collides with our character controller while a character is not moving this method is not called with access to the other game object we can program custom reactions to the collisions okay now that we have a solid understanding of these properties and methods our next step in this series on character controllers is to learn to control the built-in character controller's movement with the new input system and how to tie that together with our character's animations stay tuned for the next video coming soon in the future we'll learn all about movement with dynamic rigid bodies and not too long after we'll start taking a deep dive into unity's animation rigging package if you want to support content just like this on this channel please consider liking the video and subscribing to the channel it tells me that i'm covering content that you care about and if you want to join an awesome growing community you are invited to join the channel discord the link is in the description for updates on the next video feel free to follow me on twitter but that is all for today thank you so much for watching and i'll see you in the next video
Info
Channel: iHeartGameDev
Views: 181,560
Rating: undefined out of 5
Keywords: built-in character controller explained, built-in character controller, character movement in unity3d, how do character controllers work, move vs simplemove, simple movement in unity, characters stuck unity3d, how to use character controllers, how to make a character controller, easy movement in unity3d, how to move characters in unity3d, Built-In Character Controller Explained, How to Move Characters in Unity 3D, character controller properties, unity tutorial 3d for beginners
Id: UUJMGQTT5ts
Channel Id: undefined
Length: 13min 3sec (783 seconds)
Published: Sun Mar 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.