x


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.

more ▼

asked Dec 22 '11 at 10:09 PM

jorjdboss gravatar image

jorjdboss
78 2 3 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Dec 23 '11 at 05:10 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

That's exactly what I was looking for, thanks for the help.

Dec 23 '11 at 05:30 PM jorjdboss

Thanks! I was having trouble with this :-D

Mar 31 at 10:57 PM dogzerx2

while hitting on another colliders, charcter controller moves bit up because of physics property. How to escape from this issue?

Apr 09 at 04:23 AM sona.viswam
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x677
x204
x32

asked: Dec 22 '11 at 10:09 PM

Seen: 4254 times

Last Updated: Apr 09 at 04:23 AM