- Home /
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
Answer by Jessy · Feb 11, 2011 at 09:39 PM
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.
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.
No you're not. You're only applying gravity if you're not grounded. As I detailed, that doesn't work.
Answer by starkos · Apr 17, 2011 at 06:19 PM
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.
Your answer
Follow this Question
Related Questions
Ball Character controlled by a single Joystick? 0 Answers
Character Controller Script 3 Answers
character controller quit working, script errors 0 Answers
Object reference not set to instance of an object 2 Answers
Camera shake 2 Answers