0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity】角度制限のある Quaternion.FromToRotation を実装してみた

Last updated at Posted at 2024-10-19

概要

Quaternion.FromToRotation() に角度制限が欲しかったので関数を作りました。

下図は 45 度の制限をかけてみた例です。

gif.gif

真ん中は、右の角度に対してその方向へ45度まで向くようにしています。

コード

お手持ちの QuaternionUtil クラスに追加してご利用ください。

QuaternionUtil.cs
using UnityEngine;

public static class QuaternionUtil
{
    public static Quaternion FromToRotationLimit(Vector3 from, Vector3 to, float max)
    {
        var tangent = Vector3.Cross(from, to);
        var angle = Vector3.SignedAngle(from, to, tangent);
        return Quaternion.AngleAxis(Mathf.Min(angle, max), tangent);
    }
}

間違いや改善点がありましたらコメントをいただけると助かります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?