Unity Rigidbody2d



If you are making a 2d unity project, whether it be a platformer or another type of game, jumping is a common mechanism.

Lets start by creating an empty C# class called PlayerJump.

In this C# class, lets first grab the rigid body which allows our character to move around.

You can attach a Rigidbody to your GameObject by simply clicking on Add Component and typing in Rigidbody2D in the search field. Clicking on Rigidbody2D will attach the component to your GameObject. Now that it is attached, you will notice that many new fields have opened up.

  • Rockk.rigidbody2D.gravityScale = 0f; rockk.rigidbody2D.velocity = Vector2.zero; Also, using the rigidbody2D property is deprecated in newer versions of Unity. You might want to try something like.
  • The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be.
  • It means that you should not use rigidbody2D because it already exists as a type in Unity C# Actually, to be pedantic, rigidbody2D is not the type, it was the name of a MonoBeviour property which returns a reference of the RigidBody2D component attached to that object.
  • The bodyType property on Rigidbody is probably what you're looking for: Player.GetComponentRigidbody2D.bodyType = RigidbodyType2D.Static; It's worth mentioning that something else might be wrong if updating between minor versions of Unity is what broke it, but I wouldn't know for sure without seeing more of your project.

Also, lets create a jumpSpeed variable that will stand for how much upward velocity our character will jump with. By making this variable public, we can change the jump force of our character in the unity inspector.

Next, lets create a jump function and add the jump function so that it is called in the update function each frame.

We don’t have any code in our jump function right now so our character won’t be able to jump just yet.

Now, lets fill in our. jump function.

We use input get button down function to check when the user presses the jump button. When they press the jump button we will create a jumpVelocityToAdd vector2 and then set the velocity of the rigidbody to that variable.

The entire class is shown below. Try customizing the jumpSpeed variable to vary the amount of jump our character can have.

  • Unity Tutorial
  • Unity Useful Resources
  • Selected Reading
Unity rigidbody2d rotation
Unity

The main issue with the collisions in the last chapter was with the code. We will now modify the values of the GameObject’s position directly. We are simply adding a value to the position, if the player is pressing a key. We need a way to make the player move in such a way that it reacts properly to boundaries and other GameObjects.

To do so, we need to understand what rigidbodies are. Rigidbodies are components that allow a GameObject to react to real-time physics. This includes reactions to forces and gravity, mass, drag and momentum.

You can attach a Rigidbody to your GameObject by simply clicking on Add Component and typing in Rigidbody2D in the search field.

Clicking on Rigidbody2D will attach the component to your GameObject. Now that it is attached, you will notice that many new fields have opened up.

With the default settings, the GameObject will fall vertically down due to gravity. To avoid this, set the Gravity Scale to 0.

Unity Rigidbody2d Add Torque

Now, playing the game will not show any visible difference, because the GameObject does not have anything to do with its physics component yet.

Unity Rigidbody2d Body Type

To solve our problem, let us open our code again, and rewrite it.

Unity Rigidbody2d

We can see that we create a reference to a Rigidbody2D in the declarations, and our update code works on that reference instead of the Object’s transform. This means that the Rigidbody has now been given the responsibility of moving.

You may expect the body reference to throw NullReferenceException, since we have not assigned anything to it. If you compile and run the game as is, you will get the following error on the bottom left of the editor

To fix this, let us consider the component created by the script. Remember that public properties create their own fields in Unity, as we did with the speed variable.

Adjust the speed to a higher value, around 5, and play the game.

Unity Rigidbody2d.velocity

Your collisions will now work correctly!