Sorry in advance, I’m new to unity and C#. I have a fps style player that I can move around just fine, but when I hit other objects or a wall, it stutters weirdly. I saw something about interpolation and collision, but I tried every combination with them but have had the same results.
void Update()
{
movements = new Vector3(Input.GetAxis("Horizontal"), rb.velocity.y, Input.GetAxis("Vertical"));
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 12f;
}
else
{
speed = 6f;
}
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(rb.velocity.x, 2f, rb.velocity.z);
}
}
void FixedUpdate()
{
moveCharacter(movements);
}
void moveCharacter(Vector3 direction)
{
direction =transform.TransformDirection(direction);
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
}
Any suggestions on how to fix this problem would be greatly appreciated.
Thanks
is it top down or 3d?
does it look like the character itself is jittering on its own? or does it look like the character runs into somthing and starts to clip through, the only thing that i know of that would cause that would to use transform instead of velocity.
if its the first one
then idk
if its the second one then try tweaking how your character moves
i am defenitly not qualified to help at all but iv had these issues and this is what worked for me
Its a 3D fps game and the character jitters when it hits walls or objects when on interpolate or clips through walls when on extrapolate. What do you mean by tweak the way it moves? Like the speed? If you could give more info about what you did that would be great.
my characters always would hit a wall and start jittering and i just fixed that by changing the method of its movment. like instead of using transform.tralslate or rb.movepos or somthing like that i just tried diffrent ways of movment like RB.velocity and for me rb.vel worked but idk
and it might be the interpolation if you have it turned on
https://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html
says that
“By default interpolation is turned off. Commonly rigidbody interpolation is used on the player’s character. Physics is running at discrete timesteps, while graphics is renderered at variable frame rates. This can lead to jittery looking objects, because physics and graphics are not completely in sync.”
“By default interpolation is turned off. Commonly rigidbody interpolation is used on the player’s character. Physics is running at discrete timesteps, while graphics is renderered at variable frame rates. This can lead to jittery looking objects, because physics and graphics are not completely in sync.”
Like I said I played around with the interpolation already and when it was on interpolate, it jitters when hitting other objects ex: walls or other thing in the environment. When its on extrapolate, it does not jitter when hitting things, but instead you can clip through stuff which also is a problem.
3 things to check:
rb.MovePosition - from what I remember it attempts to teleport the object and then if it ends up inside something it will obviously get ejected out of it to correct this violation. Replace this with setting velocity directly and see if it removes the issue.
transform.position + direction * speed * Time.deltaTime - this uses transform.position as the point from which you calculate destination but be warned that transform.position and rigidbody.position are not the same. Though they may appear same at times, they can be out of synch with one another because Unity handles physics steps separately from rendering frames.
Lastly if you have your camera as a child of the rigidbody game object in the scene hierarchy, that apparently also can cause some uncomfortable jitter. I’ve seen a lot of people use a separate game object that is parented to the character not through hierarchy, but through a script that sets the rotation and position of the camera each frame. I’ve never used that approach myself, but the vast amount of tutorials where others have used it suggests it might have some truth to it.
1 Like