2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

移動する方向にオブジェクトを傾けさせたい【Unity】

Posted at

タイトルの通り。
UFOとかドローン、ゴーストみたいな浮遊してる敵とかに使えるかも?
Transform.InverseTransformDirectionという存在を初めて知った。

Simple_Tilt.cs
using UnityEngine;
public class Simple_Tilt : MonoBehaviour
{
    [SerializeField] private float xMultiple;
    private Vector3 latestPos;

    private Vector3 tiltVector;
    private Vector3 localDiff;

    void Update()
    {
        //ワールドでの移動量
        Vector3 diff = transform.position - latestPos;
        latestPos = transform.position;
        //ローカル用に変換する
        localDiff = transform.InverseTransformDirection(diff.normalized);

        tiltVector = new Vector3(
        localDiff.z * xMultiple,
        transform.eulerAngles.y,
        localDiff.x * -xMultiple);

        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(tiltVector), 0.1f);
    }
}
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?