x


Character Controller Jittering Problem

hey, i'm having some problems using the character controller, i've checked all over the web but i can't find any reason for it jittering.

this is my code:

    var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
    // We are grounded, so recalculate
     //move direction directly from axes
    if((Input.GetAxis("Horizontal") < -0.1) || (Input.GetAxis("Horizontal") > 0.1)){
        moveDirection = Vector3(0, 0, 1 * speed);
     }
    else
     {
        moveDirection = Vector3(0, 0, 0);
     }
    moveDirection = transform.TransformDirection(moveDirection);

    if (Input.GetKey ("w")) {
        moveDirection.y = jumpSpeed;
    }

}
else
{
    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;
}



// Move the controller
controller.Move(moveDirection * Time.deltaTime);

//KEEP IT IN LINE YOU SLAG!
transform.position.x = depth;

and it's all housed in a 'FixedUpdate()' function

any ideas? thanks in advance

more ▼

asked Feb 11 '11 at 07:24 PM

Freddie gravatar image

Freddie
12 3 3 6

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

2 answers: sort voted first

You have to apply "gravity" every frame, otherwise it pops out of the ground continuously. I remember providing some code to fix this at some point, on the forum, but the basic idea is that you just set moveDirection.y to the smallest negative value you can, every frame you're grounded, that fixes the issue. That way, when you walk off an edge, you aren't falling really fast immediately, which would happen if you just took

moveDirection.y -= gravity * Time.deltaTime;

out of the else statement.

And no, the scripting example has never accounted for falling off an edge properly.

Edit: Here is that forum post.

more ▼

answered Feb 11 '11 at 09:39 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

i am applying gravity each frame, i already had the code you gave in the script. the problem isn't vertical jitter it's that the controller seems to lag even though unity is running at 600fps. thanks though.

Mar 09 '11 at 09:58 PM Freddie

No you're not. You're only applying gravity if you're not grounded. As I detailed, that doesn't work.

Mar 10 '11 at 04:16 AM Jessy
(comments are locked)
10|3000 characters needed characters left

It sounds like an issue with using FixedUpdate/Update/LateUpdate. If you move your character in FixedUpdate, and move your camera in Update, you'll get quite severe jitter. Seeing as there is nothing in your code above that really requires a fixed timestep, try moving it into Update and see if that helps.

more ▼

answered Apr 17 '11 at 06:19 PM

starkos gravatar image

starkos
48 1 1 5

(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:

x3329
x1044
x534
x520
x39

asked: Feb 11 '11 at 07:24 PM

Seen: 2211 times

Last Updated: Feb 11 '11 at 07:24 PM