- Home /
Jittery Rigidbody
Hello!
I'm working on a 3D endless runner and I'm having a jitter related issue with my character's rigidbody. He is moving with the same velocity on the Z axis, but it has a lot of jitter:
Video showing the jitter - you can see the jitter better when jumping, but happens when just running as well most of the time.
Here is the setup I have in my project (settings that directly affect physics):
And here is how I control my character (script attached to the Player object, that contains the rigidbody):
void FixedUpdate()
{
// I want the player to maintain this velocity, since he can run over ramps that would slow him down.
m_rigidbody.velocity = new Vector3(_rigidbody.velocity.x, _rigidbody.velocity.y, speed);
}
Other than this I have a simple Raycast in the LateUpdate() function to check for grounded status.
if (Physics.Raycast(_rayToGround, out _groundRaycastHit, _distanceToCheckGround, distanceCheckMask))
{
// a few lines of simple code here to manage the state of the m_grounded boolean
}
For jumping I use:
public void Jump(float strength)
{
m_rigidbody.AddForce(_transform.up * strength, ForceMode.Impulse);
}
The camera follows the Player smoothly, using a Lerp function and a reasonable speed, so it is not rigidly fixed on the target.
void LateUpdate()
{
m_transform.position = Vector3.Lerp(m_transform.position, target.position + offsets, Time.deltaTime * speed);
}
No other expensive code whatsoever in my project so far.
It's worth mentioning that in Editor there is almost no jitter (I'm thinking because of better performance, I run Unity on an octacore AMD processor 4 GHz, 32gb ram and Nvidia GTX 970 video card). The device I test on (also recorded the short video) is Huawei P9 Plus (64 GB). There shouldn't be any performance issues since I setup everything in an optimal way (no lights in the scene and only custom Unlit shaders optimized for mobile platforms, all textures compressed properly, the code I write always follows strict guidelines and industry standards, no memory leaks or other issues like that).
Does anyone know what could cause all this?
Thank you!
Have you tried changing interpolate options to interpolate or extrapolate? I think that would impact jitter enormously as it smooths the movement of the rigidbody.
"For the main characters or vehicles that are followed by the camera it is recommended to use interpolation."
ref
https://docs.unity3d.com/ScriptReference/RigidbodyInterpolation.html
Answer by G3R1 · Jun 28, 2017 at 09:34 AM
If you are building this for a mobile game platform Android or iOS go to File>Build Settings>Player Settings> Other Settings and make sure multithreaded rendering is clicked(activated). This removed the jittering in my mobile game. Also if you want to increase your performance put these codes in the Awake function of your persistent gameObject if you have one:
Application.targetFrameRate = 60; (you can set it to 30 frames if you want)
Screen.SetResolution(720, 1280, true); (Resolution depends on your orientation and what your targeted device is...)
And don't forget to go in Edit>Project Settings>Quality and set "V Sync Count" to Every V Blank.
@G3R1 Thank you for your answer.
I can't tell if setting the $$anonymous$$ultithreaded rendering helped with this situation, but fixed an issue I was having someplace else, so thank you for this.
I had V Sync disabled before, setting it to Every V Blnk cases waaay more jitter than disabling it altogether.
For now I left multithreaded rendering on and disabled vertical sync, but I still have some jitter.
I'm glad it helped you a bit...What is the games fps right now ? Did you optimize the number of polygons and what shaders are you using ? If you are on mobile you should always try to use mobile shaders that are not as heavy as standart shaders of unity or something else. Also enable interpolation on your rigidbody, maybe it helps. If you turn V Sync Count Off you are going to have other problems with your device like heating up etc. That is my experience. Also make sure you set the screen resolution by script because it increases the performance way too much, at least for my game it did.
Please read my initial post, it says all that there. Also, you have screenshots of the profiler attached and the stats view in the game view attached.
Also you can see the FPS is 50/60 in the video.
Answer by Ashokkumar-M · Jun 28, 2017 at 06:22 AM
Can you try setting Application.targetFrameRate = 60; in the start of the scene in the code. Then try running in the device.
@Ashokkumar-$$anonymous$$ I am already setting the target frame rate to 60.
Answer by AngelKraft · May 07, 2021 at 12:12 PM
After searching for hours on the Internet, your post saved me :) Just for other people to know if you have some laggy movement, this did the trick for me:
Set the Rigidbody to Kinematic
Rigidbody interpolation Project
Settings > Quality > VSync Count: "Every Second V Blank"
The 3) is what made my GameObject move smoothly with rb.velocity = ...
Thanks again!
Your answer
Follow this Question
Related Questions
Resume movement of instanced object relative to the original after instantiation. 2 Answers
Manually Apply Cars Collision Response Force 0 Answers
Why is there no Rigidbody2D.SweepTest() ? 1 Answer
Add velocity relative to ground? 1 Answer
Problem Trying to Apply Non-Kinematic Velocity to Rigidbody 0 Answers