Home > database >  Does it matter if I'm moving object with rigidbody component in Update or FixedUpdate?
Does it matter if I'm moving object with rigidbody component in Update or FixedUpdate?

Time:10-07

In the end it's working fine. I just wonder if I must move gameobject with rigidbody in fixedupdate with addforce or other rigidbodies or using just update is also good ?

void Update()
    {
        switch (state)
        {
            case TransitionState.MovingTowards:
                var v = destinationTransform.position - transform.position;
                if (v.magnitude < 0.001f)
                {
                    state = TransitionState.Transferring;
                    originTransform = destinationTransform;
                    timer = 0;
                    return;
                }

                t  = Time.deltaTime;
                float s = t / duration;

                transform.position = Vector3.Lerp(originPosition,
                    destinationTransform.position, curve.Evaluate(s));

                break;

            case TransitionState.Transferring:
                timer  = Time.deltaTime;
                this.transform.position = Vector3.Lerp(originTransform.position, destinationTransform.position, timer);
                if (timer >= 1.0f)
                {
                    this.transform.parent = destinationTransform;
                    transform.localPosition = new Vector3(0, 0, 0);
                    isChild = true;
                    state = TransitionState.None;
                    this.enabled = false;
                    return;
                }
                break;

            default:
                this.enabled = false;
                return;
        }
    }

CodePudding user response:

I just wonder if I must move gameobject with rigidbody in fixedupdate with addforce or other rigidbodies or using just update is also good ?

If you are moving the object via the rigidbody component like you said (e.g. rigidbody.AddForce()) you should always use FixedUpdate.

Update happens every frame time but FixedUpdate happens every "fixed update" time that can be different from the default Update one. You usually change it as you game needs. If you need more physics processing you increate the update rate of your fixed update. If your game doesn't require a lot of physics than you can update your physics less frequently.

CodePudding user response:

Update is called on every frame. FixedUpdate is called an x amount every second. Physics calculations should always occur in FixedUpdate. Because Update is called relative to your FPS.

If you have a computer running at 30 FPS, Update is called 30 times per second. If you have a computer running at 200 FPS, Update is called 200 times per second. This means that if you Add or Subtract or do whatever to your vectors it might happen too many times. Hence why you should use FixedUpdate.

You are using Time.delteTime this is the time since Update was last called. Time.fixedDeltaTime is the time since FixedUpdate was last called. Make sure you use the right one when working from Update or FixedUpdate.

  • Related