search
LoginSignup
19
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

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

角度取得

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の固定解除

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
What you can do with signing up
19
Help us understand the problem. What are the problem?