LoginSignup
1
2

More than 5 years have passed since last update.

Update/LateUpdate/FixedUpdate

Posted at

Update

1フレームごとに実行する
複数あるUpdate関数の順番は保証されていない

LateUpdate

https://docs.unity3d.com/jp/current/ScriptReference/MonoBehaviour.LateUpdate.html
https://unity3d.com/jp/learn/tutorials/projects/roll-ball-tutorial/moving-camera?playlist=45990

すべてのUpdate関数実行後に実行される
例えば「キャラが移動 -> カメラが追跡」の順番が保証される

FixedUpdate

物理演算に使う

https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.FixedUpdate.html
https://unity3d.com/jp/earn/tutorials/projects/space-shooter/moving-the-player?playlist=45993

Physics系処理の例

PlayerController.cs
using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary 
{
    public float xMin, xMax, zMin, zMax;
}

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float tilt;
    public Done_Boundary boundary;

    Rigidbody rb;

    void Start() {
        rb = GetComponent<Rigidbody> ();
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rb.velocity = movement * speed;

        rb.position = new Vector3
        (
            Mathf.Clamp (rb.position.x, boundary.xMin, boundary.xMax), 
            0.0f, 
            Mathf.Clamp (rb.position.z, boundary.zMin, boundary.zMax)
        );

        rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
    }
}

FixedUpdateが遅いと感じた場合...
過去の位置を参照してしまい位置ずれが発生しているので、
Fixed Timestepを短くする必要がある

とはいえ、本当に物理演算が必要な場合以外は、
Updateの中で経過時間依存で移動処理を書いたほうがいい

参考
UnityのUpdate関数とLateUpdate関数の狭間でハマる
Unityでゲームを作った際に「カクカクしている」と言われないためのTimeSettings.FixedTimestep講座

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2