20
19

More than 5 years have passed since last update.

# Unity rigidbody2Dでよく使うメソッド一覧

Last updated at Posted at 2015-10-12

## 角度取得

``````transform.localEulerAngles.z;
``````

## 上下左右に押す力

``````GetComponent<Rigidbody2D> ().velocity.y; // 上下
GetComponent<Rigidbody2D> ().velocity.x; // 左右
``````

## 指定角度＆指定スピードで大砲みたいに飛ばす

``````Vector2 v;
v.x = Mathf.Cos(Mathf.Deg2Rad * direction) * speed;
v.y = Mathf.Sin(Mathf.Deg2Rad * direction) * speed;
GetComponent<Rigidbody2D>().velocity = v;
``````

## 速度や重力のスピード制限に、全方向の速度取得

``````GetComponent<Rigidbody2D> ().velocity.magnitude
``````

## 速度方向取得（移動量が大きい角度を取得）

``````dirVelocity = Mathf.Atan2 (GetComponent<Rigidbody2D>().velocity.y, GetComponent<Rigidbody2D>().velocity.x) * Mathf.Rad2Deg;
``````

## 回転させないように固定など。

``````GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezeRotation; // 回転させないように、zだけ固定
GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.FreezePosition; // x,y,z 全て固定
``````
``````GetComponent<Rigidbody2D> ().constraints = RigidbodyConstraints2D.None; // x,y,zの固定解除
``````
20
19
0

Register as a new user and use Qiita more conveniently

1. You get articles that match your needs
2. You can efficiently read back useful information
3. You can use dark theme
What you can do with signing up
20
19