Unityで球を移動している方向(rigidbodyのvelocity)に回転をさせたいのですが、今一やり方がわかりません。
どの様な感じにすればいいのでしょうか?よろしくお願いします。
※Rigidbodyの物理挙動に任せずあくまでスクリプト上で回転させたいです。
※イメージとしては塊魂の様な感じでしょうか。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答1件
0
ベストアンサー
こんな考え方でどうでしょう?
あるフレームで球がrigidbody.velocoty * Time.deltaTime
移動したとし、これをtranslation
とします。
移動距離はtranslation.magnitude
で、これをdistance
とします。
オブジェクトのスケールをscale
とすると、コライダーの本来の半径がsphereCollider.radius
ですので、球の半径はthis.sphereCollider.radius * scale
となり、弧度法による回転量はdistance / (this.sphereCollider.radius * scale)
となります。
回転軸は地面の法線(さしあたりVector3.up
とする)とtranslation
の外積の向きですので、Vector3.Cross(Vector3.up, translation).normalized
とします。
...ということで、下記のようなコードを試してみたところ、
C#
1using UnityEngine; 2 3[RequireComponent(typeof(Rigidbody), typeof(SphereCollider))] 4public class SphereController : MonoBehaviour 5{ 6 private Vector3 destination; 7 private new Rigidbody rigidbody; 8 private SphereCollider sphereCollider; 9 10 private void Start() 11 { 12 this.rigidbody = this.GetComponent<Rigidbody>(); 13 this.sphereCollider = this.GetComponent<SphereCollider>(); 14 this.rigidbody.isKinematic = true; 15 } 16 17 private void Update() 18 { 19 // とりあえず、マウスポインタの位置を球の移動目標にする 20 var plane = new Plane(Vector3.up, this.rigidbody.position); 21 var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 22 float rayDistance; 23 plane.Raycast(ray, out rayDistance); 24 if (rayDistance > 0.0f) 25 { 26 this.destination = ray.GetPoint(rayDistance); 27 } 28 29 var translation = this.rigidbody.velocity * Time.deltaTime; // 位置の変化量 30 var distance = translation.magnitude; // 移動した距離 31 var scaleXYZ = transform.lossyScale; // ワールド空間でのスケール推定値 32 var scale = Mathf.Max(scaleXYZ.x, scaleXYZ.y, scaleXYZ.z); // 各軸のうち最大のスケール 33 var angle = distance / (this.sphereCollider.radius * scale); // 球が回転するべき量 34 var axis = Vector3.Cross(Vector3.up, translation).normalized; // 球が回転するべき軸 35 var deltaRotation = Quaternion.AngleAxis(angle * Mathf.Rad2Deg, axis); // 現在の回転に加えるべき回転 36 37 // 現在の回転からさらにdeltaRotationだけ回転させる 38 this.rigidbody.MoveRotation(deltaRotation * this.rigidbody.rotation); 39 } 40 41 private void FixedUpdate() 42 { 43 var velocity = this.rigidbody.velocity; 44 this.rigidbody.MovePosition(Vector3.SmoothDamp(this.rigidbody.position, this.destination, ref velocity, 1.0f, 10.0f, Time.fixedDeltaTime)); 45 } 46}
このような動きになりました。見やすくするため地面オブジェクトを配置しましたが、動きはスクリプトで制御されているので、球が地面から外れても落下しません。
追記
angularVelocityの書き換えで回転させたい場合、オブジェクトを非キネマティックにしないとならないようです。
ですので、適宜重力を作用させなくしたり、他のオブジェクトから作用を受けないようにレイヤーを変更したりといった設定調整が必要かもしれません。
angularVelocity方式の例としては、こんな感じでどうでしょうか?
C#
1using UnityEngine; 2 3[RequireComponent(typeof(Rigidbody), typeof(SphereCollider))] 4public class NonKinematicSphereController : MonoBehaviour 5{ 6 private Vector3 destination; 7 private new Rigidbody rigidbody; 8 private SphereCollider sphereCollider; 9 10 private void Start() 11 { 12 this.rigidbody = this.GetComponent<Rigidbody>(); 13 this.sphereCollider = this.GetComponent<SphereCollider>(); 14 this.rigidbody.isKinematic = false; // 非キネマティックにする場合... 15 this.rigidbody.useGravity = false; // 重力を作用させなくする 16 } 17 18 private void Update() 19 { 20 var plane = new Plane(Vector3.up, this.rigidbody.position); 21 var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 22 float rayDistance; 23 plane.Raycast(ray, out rayDistance); 24 if (rayDistance > 0.0f) 25 { 26 this.destination = ray.GetPoint(rayDistance); 27 } 28 } 29 30 private void FixedUpdate() 31 { 32 var velocity = this.rigidbody.velocity; 33 Vector3.SmoothDamp(this.rigidbody.position, this.destination, ref velocity, 1.0f, 10.0f, Time.fixedDeltaTime); 34 // 非キネマティックに変えたので、MovePositionによる位置操作の結果が自動的にrigidbody.velocityに 35 // 反映されなくなるため、更新されたvelocityをrigidbody.velocityに再代入してやる 36 // SmoothDampの返り値は破棄し、rigidbody.velocity書き換えによってのみ移動させることでつじつまを合わせる 37 this.rigidbody.velocity = velocity; 38 39 // 先に投稿したコードのtranslationをvelocityに置き換えると、得られる回転角は 40 // 1秒あたりの弧度法による回転量...つまり角速度の大きさとなる 41 // 回転軸も前回と同様の方法で求め、これが角速度ベクトルの向きとなる 42 // あとは求めた角速度ベクトル(向きaxis、大きさangularSpeed)を 43 // rigidbody.angularVelocityに代入すればいいはず 44 45 var speed = velocity.magnitude; // 速さ 46 var scaleXYZ = transform.lossyScale; // ワールド空間でのスケール推定値 47 var scale = Mathf.Max(scaleXYZ.x, scaleXYZ.y, scaleXYZ.z); // 各軸のうち最大のスケール 48 var angularSpeed = speed / (this.sphereCollider.radius * scale); // 角速度の大きさ 49 var axis = Vector3.Cross(Vector3.up, velocity).normalized; // 角速度の向き 50 this.rigidbody.angularVelocity = angularSpeed * axis; 51 } 52}
投稿2018/07/14 00:01
編集2018/07/14 22:21総合スコア10816
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/14 13:30
2018/07/14 17:54
2018/07/14 22:02
2018/07/15 01:07