- Home /
rigidbody addforce jittering
Please tell me what I am doing wrong, I have a simple scene with a floor, a static camera and a sphere with a Rigidbody, the movement is horrible, I have tried all the solutions I could find, interpolate, modify the FixedTimeStep, etc, etc, etc.
I understand that FixedUpdate runs by default 50 times per second, everyone explains it, but no one says how to move an object with physics smoothly, they just come to the conclusion: "Now it does not tremble so much ... it's fine for me ... ". It can not be that hard...
using UnityEngine;
[RequireComponent (typeof(Rigidbody) )]
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
Vector3 movement;
public float SpeedMultiplier = 5f;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
movement.x = Input.GetAxis("Horizontal");
movement.z = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
MoveWithForce();
}
private void MoveWithForce()
{
rb.AddForce(movement * SpeedMultiplier);
}
Answer by Wil245 · Jan 28, 2019 at 04:57 PM
Well, after losing so much time with the subject, I discovered that the flaw is in the editor, in the final construction it works excellent. I thought I could not be so wrong with such a simple set of instructions ... It would be necessary to see how to report the inconvenience.
Answer by RadonRaph · Jan 10, 2019 at 10:45 PM
Hello ! Why you want to execute the deplacement in the FixedUpdate() and not in the Update ? it will run more smoother in the update() because its call more often. Raph
It will not run more smoothly in Update - quite the opposite. Fixed update is consistent, and should always be used when interfering with an object's physics. Update is called every frame, meaning physics behaviour will change on a good vs a poor machine, or during a framerate dip
Answer by Kudorado · Jan 11, 2019 at 03:14 AM
Trying change
rb.AddForce(movement * SpeedMultiplier);
to
rb.velocity = (movement * SpeedMultiplier);
it will run smoothly cause there is no affect by mass and acceleration.
Reference: https://answers.unity.com/questions/359133/why-use-addforce-rather-than-modify-velocity.html
I need to use Force, but even working on Velocity the movement is jitter...
Answer by Wil245 · Jan 11, 2019 at 01:34 PM
Hello, thanks for answering. The problem is that the direct modification of Velocity does not have the same behavior, I need Force for the calculation with the mass, with respect to putting the code in Update, according to the documentation, the physics must be handled in FixedUpdate to avoid strange behaviors, and it is true, if I do it in Update and the frame rate varies, the player crosses objects, and irregular movements of teleportation are produced... I also do not understand why you say that Update will be called more frequently, maybe I understand wrong...
Need to know how to use Addforce correctly...
Do you use a physics material ? try using a physic material without friction ?
I have tried with and without physical material, but the problem persists.
Your answer
Follow this Question
Related Questions
AddForce to position on Rigidbody 2 Answers
Confusion with AddForce with ForceMode.Acceleration 1 Answer
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce] 0 Answers
raycast hit add force problem, help needed please 1 Answer
Calculate forces required to rotate around an arbitrary point regardless of center of mass position 1 Answer