[過去ログ] 【3Dゲームエンジン】Unity質問スレッド32 (1002レス)
上下前次1-新
このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
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);
?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
省1
978(1): 2017/12/20(水)00:44 ID:TUVA4DF6(3/6) AAS
?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);
うーん、どうやっても元よりも長いコードになってしまいます。
省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
上下前次1-新書関写板覧索設栞歴
あと 23 レスあります
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.014s