- Home /
Rigid body and Character controller
I'm kinda new to Unity. I'm a programmer and I want to make a capsule move, jump, shoot etc for a 2.5d sidescroller.
Q1. Does the character controller use a Rigidbody for collision? If not, how do I have physics interaction with the world: like move crates around, push a ball etc?
Q2. Does the character controller force capsule collider or can we customize it?
Q3. I found this code in the docs: http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html If I use it for a sidescroller, the character can't move while in air and the motion is unrealistic, any solution for this? Also, can you help me out with multiple jumps in the air without touching the ground.
Answer by aldonaletto · Dec 23, 2011 at 05:10 AM
1- No, the CharacterController doesn't use a Rigidbody (and attaching a rigidbody to it usually produce very weird results). You can use OnControllerColliderHit to push rigidbodies (see the example in the docs). To carry an object, you can child it to the character - but don't let it be in touch with the capsule collider, because it can disturb the character movement (it's a CharacterController bug).
2- The CharacterController has a built in capsule collider, and we can't change this;
3- The Move example doesn't allow control during the jump, but this can be changed if you store the vertical speed in a float variable and rearrange the code a little:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero; private var vSpeed : float = 0; // keep vertical speed in a separate variable private var controller: CharacterController; // controller reference
function Update() { if (!controller) controller = GetComponent(CharacterController); moveDirection = transform.right Input.GetAxis("Vertical") speed; if (controller.isGrounded) { vSpeed = 0; // a grounded character has zero vert speed unless... if (Input.GetButton ("Jump")) { // unless Jump is pressed! vSpeed = jumpSpeed; } } // Apply gravity vSpeed -= gravity Time.deltaTime; moveDirection.y = vSpeed; // include vertical speed // Move the controller controller.Move(moveDirection Time.deltaTime); } This code only allows the character to jump when grounded; if you want it to jump even when in the air, move the jump code outside the isGrounded if
That's exactly what I was looking for, thanks for the help.
while hitting on another colliders, charcter controller moves bit up because of physics property. How to escape from this issue?
@sona.viswam simplest way would be to put a downward force, cant tell much without seeing the code you put or the settings on your controller