LoginSignup
34
38

More than 5 years have passed since last update.

Unityでオブジェクトを指定の方向に向ける

Last updated at Posted at 2017-02-16

オブジェクトを指定の方向に向けたい

lookat1.jpg
図のようにCubeをSphereの方向に向かせたい場合
transform.forwardに方向ベクトルを突っ込む方法とtransform.LookAt関数を使う方法がある

検証コード1

LookAtTest.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookAtTest : MonoBehaviour 
{
    public Transform _target;

    readonly int COUNT = 100000;

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            float startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 direction = (_target.position - this.transform.position).normalized;
                this.transform.forward = direction;
            }
            float end1 = Time.realtimeSinceStartup - startTime;

            startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 direction = (_target.position - this.transform.position);
                this.transform.forward = direction;
            }
            float end2 = Time.realtimeSinceStartup - startTime;

            startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                this.transform.LookAt (_target.position);
            }
            float end3 = Time.realtimeSinceStartup - startTime;

            Debug.Log (end1);    //0.04388928sec
            Debug.Log (end2);    //0.03444195sec
            Debug.Log (end3);    //0.02334642sec
        }
    }
}

手法 実行時間
forwardに方向ベクトル normalizeあり 0.04388928sec
forwardに方向ベクトル normalizeなし 0.03444195sec
LookAt関数 0.02334642sec

何も考えずにLookAt関数を使うのが一番早い
forwardに突っ込む場合もわざわざnormalize(正規化)しなくても大丈夫

オブジェクトを軸固定で指定の方向に向けたい

lookat2.jpg
図のようにCubeをSphereの方向にY軸だけ回転させて向かせたい場合
外積を用いる方法、transform.LookAt関数のtargetの座標Yを一致させる方法
targetの座標Yを一致させ、Quaternion.LookRotationを使う方法がある

検証コード2

LookAtTest2.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LookAtTest2 : MonoBehaviour 
{
    public Transform _target;

    Vector3 _RotAxis = Vector3.up;

    readonly int COUNT = 100000;

    void Update()
    {
        if (Input.GetKeyDown (KeyCode.S)) 
        {
            float startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 direction = (_target.position - this.transform.position).normalized;
                Vector3 xAxis = Vector3.Cross (_RotAxis, direction).normalized;
                Vector3 zAxis = Vector3.Cross (xAxis, _RotAxis).normalized;
                this.transform.rotation = Quaternion.LookRotation(zAxis,_RotAxis);
            }
            float end1 = Time.realtimeSinceStartup - startTime;

            startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 direction = (_target.position - this.transform.position);
                Vector3 xAxis = Vector3.Cross (_RotAxis, direction);
                Vector3 zAxis = Vector3.Cross (xAxis, _RotAxis);
                this.transform.rotation = Quaternion.LookRotation(zAxis,_RotAxis);
            }
            float end2 = Time.realtimeSinceStartup - startTime;

            startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 target = _target.position;
                target.y = this.transform.position.y;
                this.transform.LookAt (target);
            }
            float end3 = Time.realtimeSinceStartup - startTime;

                        startTime = Time.realtimeSinceStartup;
            for (int i = 0; i < COUNT; i++) {
                Vector3 target = _target.position;
                target.y = this.transform.position.y;
                this.transform.rotation = Quaternion.LookRotation (target , _RotAxis);
            }
            var end4 = Time.realtimeSinceStartup - startTime;

            Debug.Log (end1);    //0.06525421sec
            Debug.Log (end2);    //0.03794098sec
            Debug.Log (end3);    //0.03161621sec
                        Debug.Log (end4);    //0.03028011sec
        }
    }
}

手法 実行時間
外積 normalizeあり 0.06525421sec
外積 normalizeなし 0.03794098sec
LookAt関数 0.03161621sec
LookRotation関数 0.03028011sec

LookRotation関数の方がLookAt関数よりわずかに早い

参考リンク

34
38
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
34
38