【3Dゲームエンジン】Unity質問スレッド32

レス数が950を超えています。1000を超えると書き込みができなくなります。
1名無し@キムチ
垢版 |
2017/10/30(月) 05:05:35.00ID:xnHTCaF3
フリー版で開発したゲームは個人・法人を問わず追加のロイヤリティーなしで販売できます。
プラットフォームサポートはiPhone/iPod Touch/iPad、Mac、PC、Web、Wii、Xbox360、Android、PlayStation 3、PlayStation 4、PS Vita他

次スレは>>950以降にできるだけ早く(できれば回答者が回答した後にでも余裕を持って)建てて下さい。

■Unity 2ch Wiki(まとめ、過去ログ、リンク集)
http://www24.atwiki.jp/unity2ch/

●リンク
・Unity(日本公式サイト)
http://unity3d.com/jp
・マニュアル
 http://docs.unity3d.com/ja/current/Manual/index.html
・チュートリアル
 http://unity3d.com/jp/learn/tutorials
・スクリプトリファレンス
 http://docs.unity3d.com/ja/current/ScriptReference/index.html
・FAQ
 http://unity3d.com/jp/unity/faq
・MSDN
 http://msdn.microsoft.com/ja-jp/ms348103←スクリプトのエラー番号をこれで検索すると原因が日本語で出てきます。

前スレ
【3Dゲームエンジン】Unity質問スレッド31
https://mevius.5ch.net/test/read.cgi/gamedev/1504366121/
2017/12/20(水) 00:34:43.35ID: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; ←ここを置き換えるコードです。
 }
}
2017/12/20(水) 00:39:41.23ID:TUVA4DF6
@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
 transform.rotation = transform.rotation * Quaternion.Euler(0, 0, Mathf.Sign(Vector3.Dot(transform.forward, Vector3.Cross(transform.right, direction))) *Vector3.Angle(transform.right, direction));
2017/12/20(水) 00:44:36.57ID:TUVA4DF6
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);

うーん、どうやっても元よりも長いコードになってしまいます。

オブジェクトAの位置が原点だとか、オブジェクトAの向きが無回転だとか、
条件を付ければ、もう少し簡略化できますが・・・
2017/12/20(水) 01:00:49.24ID: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軸回転成分だけを取り出して減算します。
レス数が950を超えています。1000を超えると書き込みができなくなります。