LoginSignup
0
1

More than 3 years have passed since last update.

Unityで2つのベクトルの向きが同じになるように回転させる

Last updated at Posted at 2019-12-03

はじめに

右手と左手間のベクトルに合うようにいい感じにコントローラを回転させています。

これはUnityでスクリプトからオブジェクトを回転させるときに
そのオブジェクトが持つベクトルとあるベクトルの向きを同じになるように回転させました。

たまに使うので備忘録です。

サンプルコードとイメージ図

image.png

p3とp4のベクトルの向きがp1とp2のベクトルの向きに合うようにp3とp4を持つオブジェクトを回転させます。

VectorRotationSample.cs
    // Vector3 p1, p2, p3, p4;
    // GameObject target;
    Vector3 refVec = (p1 - p2).normalized; // これと同じになるように
    Vector3 vec = (p3 - p4).normalized; // これを動かす(p3, p4はtargetに含まれる任意の2点)
    Quaternion rot = Quaternion.FromToRotation(refVec, vec);

    target.transform.rotation = rot * target.transform.rotation; // 左から掛ける

関連文献

【Unity】Quaterion API解説

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