【Unity】お手軽ビルボードスクリプト

DEVELOP

毎回書くのも手間なので、置いておきます。

using UnityEngine;
/// <summary>
/// ビルボード
/// ObjectのRotationを操作します
/// </summary>
[ExecuteInEditMode]
public class Billboard : MonoBehaviour
{
    [SerializeField, Header("対象のカメラ")]
    private Camera m_TargetCamera;
    public Camera targetCamera
    {
        get { return m_TargetCamera; }
        set { m_TargetCamera = value; }
    }
    private void LateUpdate()
    {
        if (targetCamera == null)
        {
            return;
        }
        // カメラの向いている方向ベクトル
        var cameraV = targetCamera.transform.rotation * Vector3.forward;
        var reverse = cameraV * -1f;
        transform.localRotation = Quaternion.FromToRotation(Vector3.back, reverse);
    }
}

一部回転させない

// x固定
reverse.z = 0;
transform.localRotation = Quaternion.FromToRotation(Vector3.back, reverse);

Posted by kazupon