Help us understand the problem. What is going on with this article?

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

はじめに

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

これは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解説

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした