1

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; } } 
2
  • As you are doing nothing using physics it is fine. Only physics calculations should happen in FixedUpdate. If this object has a rigidbody, if you want it to react to other objects in a physically accurate way, then use physics calculations for movement. Once you do this, move the code to FixedUpdate. Commented Oct 6, 2021 at 17:23
  • 1
    If your object has a Rigidbody attached you should really only move it using the Rigidbody itself. Mixing direct Transform manipulation and Rigidbodies is generally a a bad idea, since it can produce weird interactions between objects. Also you should always use FixedUpdate for physics-related stuff since it is framerate-indipendent and always called before physics updates (collisions etc.) occur. This might be helpful. Commented Oct 6, 2021 at 17:27

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For your given example it is not necessary to use FixedUpdate because you are not moving it via the rigidbody component, but it is a bad practice if your object has a rigidbody attached to it.
1

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.

Comments

0

in total, you should use fixed update when you are working with physics. if you use update your game performance of physics can vary from the system which runs your game on 30fps and the one that runs 120fps and it would break your whole physics calculation as the other answers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.