Tag

Rigidbody2D Archives - Selfloop

Understanding : Rigidbody2D component

By | Unity3D | No Comments

Definition: Rigidbody2D is a component that activate the game object under the unity physics engine. So by adding the Rigidbody2D, the game object get pulls by gravity, start having collision etc. When you attached the Rigidbody2D component it overrides the transform component for position and rotation. So it is better to do the translation and rotation from “Rigidbody2D” when it is attached with the following game object.

  • Body Type:

 Rigidbody2D

There are basically 3 types of “Body Type”,

These three types of body have effect on movement and collision detection mechanism.

Dynamic: Dynamic body type is the most performance expensive body type of the rigidbody2D as it is effected by all the behavior of the physics such as collision detection, mass and specially dealing with the gravity and force. So if any game object would like to have the most realistic physics mechanism, “Dynamic” body type is the way to go.

 

Kinematic: Kinematic body type is more processor efficient as it doesn’t effect by the gravity and force, where it still can detect the collision. The outer force cannot have any impact as the game object itself assigned an infinite mass to itself. But you can defiantly move & rotate these types of game object by Rigidbody2D.MovePosition & Rigidbody2D.MoveRotation.

Use Full Kinematic: When enabling this setting, it will detect the collision with other body type as well (Dynamic, Static: Same collision detection mechanism as Dynamic). The only difference with “Dynamic” body type that it has infinite amount of mass that cannot be effect by gravity or force.

 

Static: Static body is the least processor cost physics behavior as it also has infinite mass assigned to itself. So basically, a boss that cannot be knock back can be an example of the static behavior.

  • Material: We have talked about the material on the “Collider2D” component section, please check the following link for more information.
  • Simulated: Its basically enabling/disabling the physics behavior (Including Rigidbody2D, collider2D join2D) of the game object rather than enabling/disabling each of the component individually. Its more memory efficient than disabling every collider2D individually as creating and destroying a behavior is memory expensive.
  • Use Auto Mass: Auto mass calculate the mass of the game object by the size of the collider. Let us consider the “BoxCollider2D”. Here the size is denoted by (X,Y). So the mass that would be generate by auto mass = X * Y (3 x 2 = 6 mass).
  • Linear Drag: It is basically the friction of the velocity over the X & Y axis. The higher the value, the higher the friction for the velocity.
  • Angular Drag: It is basically the friction of the rotation over the X & Y axis. The lower the value, the higher the rotation by the environmental force or collision.
  • Gravity Scale: it is the coefficient of the gravity that how strong the gravity would be over the game object. If the gravity scale is “2.5”, so the gravity would 2.5x (9.8 x 2.5 = 24.5ms^-2) stronger.
  • Collision Detection: it is basically how frequently the collision will detect. There are two ways of setting that a collision can be detected.
  1. Discrete: It basically check the collision over the physics update. Physics update are basically the “FixedUpdate()” that we used in the script. This update happens after fixed period of time. Like in every 0.2sec, the physics engine will start detecting collision.
  2. Continuous: It basically check the collision on every update. The “Update()” method gets called in a random interval of time based on how much process it has to do for generating the next frame. The call of this update are not after a fixed amount of time. For example, the next 4 frame can be load -> 0.025, 0.033, 0.0.75, 0.9. So it is more efficient when the collision detection is sensitive for any game object (Could be player?).
  • Sleeping Mode: there are 3 types of sleeping mode.
  1. Never Sleep: You should not choose this mode as it can hamper the resource optimization.
  2. Start Awake: The game object can start detecting collision from “Awake()” stage.
  3. Start Asleep: The game object will initially sleep, but will be awake by the first collision detection.
  • Constraint: It is useful to freeze the rotation over the Axis.

 

Methods: Some of the methods that we frequently use in game rigidbody2D,

  1. Rigidbody2D.AddForce.
  2. Rigidbody2D.Velocity.

 

  • Rigidbody2D.AddForce(): The parameter of the “AddForce” is a Vector2, that denotes in which direction the force is going to apply and how much force it is going to apply to that certain direction. Let us consider, we want to apply force to the positive Y-Axis to make the player jump by pressing the “Space” key from the keyboard. For that, we will write the following code,

If(Input.GetKeyDown(KeyCode.Space)){

//_rigidbody2D is holding the reference of the Rigidbody2D component of

// attached game object.

_rigidbody2D.AddForce(new Vector2(0.0f , 500.0f));

}

Here “500” is the amount of force we want to apply to a certain direction, and the value is positive cause we want to apply the force to the positive Y-Axis.

 

  • Rigidbody2D.Velocity(): The parameter of the “Velocity” is also Vector2, that denotes in which direction we want to apply our velocity and how much velocity we want to apply to that specific direction. Remember, the velocity is the result of the force. When you are changing the velocity it means the space is putting the force to that certain direction on your game object. Let us consider we want to move the character to the right side by pressing the “Right Arrow Key”. So changing the velocity over the positive X-Axis will do our trick.

If(Input.GetKeyDown(KeyCode.RightArrow)){

//_rigidbody2D is holding the reference of the Rigidbody2D component of

// attached game object.

_rigidbody2D.velocity = new Vector2(2.0f, _rigidbody2D.velocity.y);

}

Here, the we are constantly updating the velocity (2.0f) over the positive X-axis while keeping the velocity over Y-axis as it is.


# The reference of the documents are taken from the “Unity – Documentation”.

Muhammad Tashfiqul Bari

Mentor at Coursera