- #Unity 3d character controller update#
- #Unity 3d character controller code#
- #Unity 3d character controller series#
#Unity 3d character controller code#
Insert the following code at the end of Update. Unity provides a method to find that distance for us, Collider2D.Distance. Ideally, we should move it the minimum distance required to be no longer touching the other collider. The main problem is to decide which direction, and how far, we need to translate our controller to depenetrate from each collider. To accomplish this, we will iterate over every collider we intersected, and push the controller out of each offending collider in turn.
2.1 ResolutionĪt the end of collision resolution, we want our controller to be no longer intersecting any other colliders. Note that because of this, the array will also contain our own BoxCollider. This will give us an array of all colliders that are intersected by the box we defined, which is the same size as our BoxCollider and at the same position. Collider2D hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0) Insert the following code at the bottom of Update.
#Unity 3d character controller series#
Unity provides a series of Physics2D overlap functions to help detect intersections. We should do this after we have translated our controller to ensure no further movement will occur after we resolve collisions. Our goal is to find all colliders our controller is currently touching.
This method tells Unity to move a transform, but does not tell it to handle collision detection or resolution. This is happening due to our decision to use Transform.Translate to move around. The controller's side to side movement is working great, but it's currently able to pass through walls. Velocity.x = Mathf.MoveTowards(velocity.x, 0, groundDeceleration * ltaTime) Replace the line modifying velocity.x with the follow if statement. We can handle this by checking to see if moveInput has a non-zero value. This is fine, but we might want to have the deceleration rate different than our walkAcceleration. Note that when no keys are being pressed, moveInput will be zero, causing our controller to slow to a stop. Mathf.MoveTowards is being used to move our current x velocity value to its target, our controller's speed (in the direction of our sampled input). Velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, walkAcceleration * ltaTime) float moveInput = Input.GetAxisRaw("Horizontal")
#Unity 3d character controller update#
Add the following at the top of the Update method.
Let's change that by adding some horizontal velocity when the left or right keys are pressed.
Our velocity isn't being modified yet, so our controller won't move. This will translate the controller by velocity every frame, multiplied by deltaTime to ensure our game is framerate independent This is a lot to ask from a capsule-shaped object that is designed to react to the world in the way a real-world capsule would.īy instead directly modifying the position of our controller, we are able to very finely tune exactly how it interacts with the world, which is essential to crafting a game that feels and plays fluidly.Īdd the following line of code in the Update method. Characters often need to be able to run, slide, crouch, climb-all possibly with different acceleration and friction values dependant on their current state, or the surface they are interacting with. However, something like a capsule rigidbody would be poor at representing an object capable of very complex actions, like a character controller. Using constraints and joints, they can be used to model more complex objects, like a multi-limb robot. Box rigidbodies will excel at representing a wooden crate, cylinder rigidbodies as a an oil drum, and so on. Rigidbodies behave very similarly to the real world objects they represent. So why not use them for character controllers? Both of these extensively support rigidbodies. Unity comes packaged with two physics engines: PhysX for 3D, and Box2D for 2D.