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.

Watch this video in context on Unity's learning pages here -Rigidbody2D component places. And thank you for taking the time to help us improve the quality of Unity Documentation. Your name Your email. The physical behaviour type of the Rigidbody2D. Controls the physical behaviour of a Rigidbody2D in how it should move, react to forces and interact with the rest of the physics simulation. See Also: Rigidbody2D.bodyType. Using RigidBody AddForce method to move Game Object in Unity. While normal movement of gameobject is possible by changing its position coordinates, for rigid bodies we can also use some built-in methods to enhnance movement effects of game objects. There are mainly two ways of moving a gameObject in Unity: Changing Position Coordintes: By directly changing the position of a gameObject without much consideration to its physics or other such components. This is what we've been doing so far, by simply adding a value to the object's X position every frame.; Rigidbody Physics: When dealing with objects that follows physics rules, it makes.

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. Transformers 5 full movie in tamil hd download.

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
GetcomponentUnity Rigidbody2d
  • Unity Useful Resources
  • Selected Reading

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.

Unity Rigidbody2d Moveposition

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.

Unity Getcomponent Rigidbody2d

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.

Unity Rigidbody2d Movement

Unity

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

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

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

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 Add Torque

Your collisions will now work correctly!