[過去ログ] 【3Dゲームエンジン】Unity質問スレッド32 (1002レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
976
(1): 2017/12/20(水)00:34 ID:TUVA4DF6(1/6) AAS
>>969

考えてみました。
Aに下記のスクリプトをセットします。

public class A : MonoBehaviour {
 GameObject obj;
 Vector3 target, position, direction;

 void Start () {
省10
977
(1): 2017/12/20(水)00:39 ID:TUVA4DF6(2/6) AAS
@Quaternion.FromToRotationを使用する方法1
 transform.rotation = Quaternion.FromToRotation (Vector3.right, direction);

AQuaternion.FromToRotationを使用する方法2
 transform.rotation = transform.rotation * Quaternion.FromToRotation (transform.right, direction);

BVector3.Angleを使用する方法1
 transform.rotation = Quaternion.Euler(0, 0, Mathf.Sign(Vector3.Dot(Vector3.forward, Vector3.Cross(Vector3.right, direction))) *Vector3.Angle(Vector3.right, direction));

CVector3.Angleを使用する方法2
省1
978
(1): 2017/12/20(水)00:44 ID:TUVA4DF6(3/6) AAS
DVector3.Angleを使用する方法3(Rotateを使用)
 transform.Rotate (0, 0, Mathf.Sign(Vector3.Dot(Vector3.forward, Vector3.Cross(transform.right, direction))) *Vector3.Angle(transform.right, direction));

EMathf.Atan2を使用する方法1(Rotateを使用)
 transform.Rotate (0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - transform.rotation.eulerAngles.z);

FMathf.Atan2を使用する方法2
 transform.rotation = Quaternion.AngleAxis(Mathf.Atan2((direction.y, direction.x) * Mathf.Rad2Deg, Vector3.forward);

うーん、どうやっても元よりも長いコードになってしまいます。
省2
979
(1): 2017/12/20(水)01:00 ID:TUVA4DF6(4/6) AAS
Quaternion.FromToRotation(A, B)では、
回転方向を含めた回転状態を得られますが、

Vector3.Angle(A, B)では、
ベクトル間の角度しかわからず、向きがわかりません。
また、取得される角度は180度以下に限定されるため、
左向きに270度は、右向きに90度かもしれないということです。

回転方向は、Vector3.Cross(A, B)のZ成分が正か負かで判定します。
省8
982: 2017/12/20(水)22:49 ID:TUVA4DF6(5/6) AAS
>>980

テラシュールブログ
外部リンク:tsubakit1.hateblo.jp

ここに、Quaternion.FromToRotationの使い方がうまくまとまってました。
>>var diff = (target.position - transform.position ).normalized;
>>transform.rotation = Quaternion.FromToRotation( Vector3.up, diff);

このサイトでは、diffは正規化されているようですが、
省4
983: 2017/12/20(水)22:56 ID:TUVA4DF6(6/6) AAS
なお、私の方法では、外積ベクトルとZ軸の内積をとっていますが、
最終的に符号のみを取り出しているので、大きさが影響しません。

Vector3.Reflect,Vector3.Project,Vector3.ProjectOnPlaneなど、
単位ベクトルに内積を乗算するタイプの関数では、
必ず正規化して単位ベクトルにしてから代入しないと、
計算結果が変わってきますので、注意が必要です。
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.029s