angularVelocity = 360
で1秒1回転となることについては、angularVelocityのリファレンスページ にある...
角度 / 秒で示される角速度
との記述から説明できるかと思います。angularVelocity = 360
というのは秒速360°を表しますので、つまり毎秒1回転というわけですね。
制限速度を超えないようにAddTorque
を行う件については、AddTorqueのリファレンスページ に例示されている...
C#
1 using UnityEngine ;
2
3 public class TorqueRotationExample : MonoBehaviour
4 {
5 // Add an impulse which produces a change in angular velocity (specified in degrees).
6 public void AddTorqueImpulse ( float angularChangeInDegrees )
7 {
8 var body = GetComponent < Rigidbody2D > ( ) ;
9 var impulse = ( angularChangeInDegrees * Mathf . Deg2Rad ) * body . inertia ;
10
11 body . AddTorque ( impulse , ForceMode2D . Impulse ) ;
12 }
13 }
がご参考になるんじゃないでしょうか。あれをベースに、一例としては下記のようなコードでどうでしょう。
C#
1 using UnityEngine ;
2
3 [ RequireComponent ( typeof ( Rigidbody2D ) ) ]
4 public class AddTorqueTest : MonoBehaviour
5 {
6 // 角速度の限界の絶対値
7 [ SerializeField ] [ Range ( 0.0f , 1440.0f ) ] private float angularVelocityLimit = 720.0f ;
8
9 private new Rigidbody2D rigidbody ;
10
11 private void Start ( )
12 {
13 this . rigidbody = this . GetComponent < Rigidbody2D > ( ) ;
14
15 // 挙動の確認のための下準備として、Start時にランダムな角速度を与えておく
16 this . rigidbody . angularVelocity = Random . Range ( - 360.0f , 360.0f ) ;
17 }
18
19 private void Update ( )
20 {
21 if ( Input . GetKeyDown ( KeyCode . Space ) )
22 {
23 // 現在の角速度を取得し...
24 var currentAngularVelocity = this . rigidbody . angularVelocity ;
25
26 // 現在の角速度に対して1秒間に1回転...つまり360°/秒の
27 // 角速度変化を与えると、新しい角速度はこうなる
28 var newAngularVelocity = currentAngularVelocity + 360.0f ;
29
30 // 新しい角速度が限界角速度を超えないようクランプし...
31 newAngularVelocity = Mathf . Clamp ( newAngularVelocity , - this . angularVelocityLimit , this . angularVelocityLimit ) ;
32
33 // 新しい角速度から現在の角速度を引いて角速度変化とする
34 var angularVelocityChange = newAngularVelocity - currentAngularVelocity ;
35
36 // 角速度変化を弧度法に直して、さらに慣性モーメントをかけると角運動量変化が得られる
37 var impulse = angularVelocityChange * Mathf . Deg2Rad * this . rigidbody . inertia ;
38
39 // オブジェクトの角運動量を変化させる
40 this . rigidbody . AddTorque ( impulse , ForceMode2D . Impulse ) ;
41 }
42 }
43 }