[過去ログ]
【3Dゲームエンジン】Unity質問スレッド32 (1002レス)
【3Dゲームエンジン】Unity質問スレッド32 http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
必死チェッカー(本家)
(べ)
自ID
レス栞
あぼーん
このスレッドは過去ログ倉庫に格納されています。
次スレ検索
歴削→次スレ
栞削→次スレ
過去ログメニュー
976: 名前は開発中のものです。 [sage] 2017/12/20(水) 00:34:43.35 ID:TUVA4DF6 >>969 考えてみました。 Aに下記のスクリプトをセットします。 public class A : MonoBehaviour { GameObject obj; Vector3 target, position, direction; void Start () { obj = GameObject.Find ("B"); target = obj.transform.position; position = transform.position; direction = target - position; Invoke ("Rotate", 1.0f); } void Rotate(){ transform.right = direction; ←ここを置き換えるコードです。 } } http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/976
977: 名前は開発中のものです。 [sage] 2017/12/20(水) 00:39:41.23 ID:TUVA4DF6 ?Quaternion.FromToRotationを使用する方法1 transform.rotation = Quaternion.FromToRotation (Vector3.right, direction); ?Quaternion.FromToRotationを使用する方法2 transform.rotation = transform.rotation * Quaternion.FromToRotation (transform.right, direction); ?Vector3.Angleを使用する方法1 transform.rotation = Quaternion.Euler(0, 0, Mathf.Sign(Vector3.Dot(Vector3.forward, Vector3.Cross(Vector3.right, direction))) *Vector3.Angle(Vector3.right, direction)); ?Vector3.Angleを使用する方法2 transform.rotation = transform.rotation * Quaternion.Euler(0, 0, Mathf.Sign(Vector3.Dot(transform.forward, Vector3.Cross(transform.right, direction))) *Vector3.Angle(transform.right, direction)); http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/977
978: 名前は開発中のものです。 [sage] 2017/12/20(水) 00:44:36.57 ID:TUVA4DF6 ?Vector3.Angleを使用する方法3(Rotateを使用) transform.Rotate (0, 0, Mathf.Sign(Vector3.Dot(Vector3.forward, Vector3.Cross(transform.right, direction))) *Vector3.Angle(transform.right, direction)); ?Mathf.Atan2を使用する方法1(Rotateを使用) transform.Rotate (0, 0, Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - transform.rotation.eulerAngles.z); ?Mathf.Atan2を使用する方法2 transform.rotation = Quaternion.AngleAxis(Mathf.Atan2((direction.y, direction.x) * Mathf.Rad2Deg, Vector3.forward); うーん、どうやっても元よりも長いコードになってしまいます。 オブジェクトAの位置が原点だとか、オブジェクトAの向きが無回転だとか、 条件を付ければ、もう少し簡略化できますが・・・ http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/978
979: 名前は開発中のものです。 [sage] 2017/12/20(水) 01:00:49.24 ID:TUVA4DF6 Quaternion.FromToRotation(A, B)では、 回転方向を含めた回転状態を得られますが、 Vector3.Angle(A, B)では、 ベクトル間の角度しかわからず、向きがわかりません。 また、取得される角度は180度以下に限定されるため、 左向きに270度は、右向きに90度かもしれないということです。 回転方向は、Vector3.Cross(A, B)のZ成分が正か負かで判定します。 2Dでは、Mathf.Sign((Vector3.Cross(A, B)).z)でも良いのですが、 3Dでは、回転面がXY平面上とは限らないため、外積ベクトルと 正の向きにしたい軸との内積をとってから符号判定した方が確実です。 Atan2関数は、戻り値がradのため、度に直す必要があります。 また、相対回転であるRotateを使用する場合は、 求めた偏角から、現在の向きの回転角を引いておく必要があります。 rotationはQuaternionのため、eulerAnglesでオイラー角に戻しておき、 Z軸回転成分だけを取り出して減算します。 http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/979
982: 名前は開発中のものです。 [sage] 2017/12/20(水) 22:49:28.74 ID:TUVA4DF6 >>980 テラシュールブログ http://tsubakit1.hateblo.jp/entry/2014/08/02/030919 ここに、Quaternion.FromToRotationの使い方がうまくまとまってました。 >>var diff = (target.position - transform.position ).normalized; >>transform.rotation = Quaternion.FromToRotation( Vector3.up, diff); このサイトでは、diffは正規化されているようですが、 基本的に内部で強制的に単位ベクトルになるようなので、 target.position - transform.positioだけで大丈夫そうです。 なお、外積ベクトルを利用する場合は、大きさも影響してきますので、 方向ベクトルは単位ベクトルにしておいたほうが無難です。 http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/982
983: 名前は開発中のものです。 [sage] 2017/12/20(水) 22:56:36.28 ID:TUVA4DF6 なお、私の方法では、外積ベクトルとZ軸の内積をとっていますが、 最終的に符号のみを取り出しているので、大きさが影響しません。 Vector3.Reflect,Vector3.Project,Vector3.ProjectOnPlaneなど、 単位ベクトルに内積を乗算するタイプの関数では、 必ず正規化して単位ベクトルにしてから代入しないと、 計算結果が変わってきますので、注意が必要です。 http://mevius.5ch.net/test/read.cgi/gamedev/1509307535/983
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.033s