- Home /
FixedUpdate is used only for physics objects. How exactly?
So, is it ONLY used for physics objects, or there are exceptions? Here, take this code that I made, where I try to rotate the camera around a cursor by 90 degrees after user input, for instance:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public Transform cursor;
private bool rotate = false;
private float alreadyRotated = 0;
void Rotate () {
transform.RotateAround(cursor.position, Vector3.up, 90 * Time.deltaTime);
alreadyRotated += 90 * Time.deltaTime;
if(Mathf.Approximately(alreadyRotated, 90))
{
rotate = false;
alreadyRotated = 0;
}
}
void Start () {
transform.position = new Vector3(8, 3, 12);
transform.LookAt(cursor);
}
void Update () {
if(Input.GetKey("1"))
{
rotate = true;
}
}
void FixedUpdate () {
if(rotate)
Rotate();
}
}
I'm not dealing with physics here, right? If I do call this inside a Update(), however, the Camera never stops rotating, while inside a FixedUpdate() it does (this is what I want). I do get it, however, that this is because FixedUpdate gives me more precise results, but I was just questioning myself... Is it right to limit the use of FixedUpdate to RigidBody use, only? I've seen this being said more than a couple of times already... Please enlighten me, guys! Thanks :)
Answer by tanoshimi · Feb 03, 2014 at 11:05 PM
Did you try searching? http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html
Hi tanoshimi, thanks for the answer. I've actually read that post sometime ago. He basically says what I'm questioning:
It's for this reason that FixedUpdate should be used when applying forces, torques, or other physics-related functions - because you know it will be executed exactly in sync with the physics engine itself.
Whereas Update() can vary out of step with the physics engine, either faster or slower, depending on how much of a load the graphics are putting on the rendering engine at any given time, which - if used for physics - would give correspondingly variant physical effects!
I may be wrong, but the rotation I'm perfor$$anonymous$$g, can be considered a physics function? Because it (my rotation) only works with FixedUpdate. This is the point I'm not getting...
It's not a physics function. The reason it won't work in Update is because you're using $$anonymous$$athf.Approximately, which is only for very tiny values. It's highly unlikely that you will get a result that's almost exactly 90.
Then this is a special scenario where FixedUpdate will work and Update won't? Thanks for the answer, Eric5h5!
No, FixedUpdate isn't appropriate, and can still fail. You should use logic that doesn't depend on two float values being equal (or nearly so).
I see. Thanks. Any suggestions as to what I should use to make a camera move 90 degrees around other object, after a user presses 1?