JUMP in UNITY! | FIRST GAME | PLATFORMER 2D EASY

Published: 11 January 2024
on channel: Kotorman Entertainment
85
2

Hi! In previous tutorial we learned how to create a new project and make simple movement for our character. Today we are going to improve that script and add cool jumping ability. So, Let's start!

Google Play: https://play.google.com/store/apps/de...

0:00 Intro
0:20 Input Manager
1:19 Change Input Manager keys
1:33 Serialization & SerializeField
1:55 GetComponent & how it works
2:35 Simple Jump
4:07 Disable rotation
4:18 Perfect Jump logic
6:22 Outro

Hi! In previous tutorial we learned how to create a new project and make simple movement for our character. Today we are going to improve that script and add cool jumping ability.

In previous video, we made a movement mechanic using key pressing, but there is a much better decision. Type float input x equals Input get axis Horizontal. Now our float will take a number from pressing keys. When we press A or arrow left, it will smoothly change to minus one. If you press D or arrow right, the value will rize to one. In other cases function will return zero.

If you want to change buttons, you should go to Edit, Project Settings, Input Manager and click on dropdown menu. There are dozens of defined controls, and all of them could be changed at any time as you want.

So instead of checking key press conditions, we can fit it all into one line: transform translate Vector 3 right multiplied by speed, multiplied by input x and finally multiplied by Time delta time. Clear or comment all previous code.
Run the game. As you see, it works, and in addition our character moves smoother than before. If you don’t like that, you can change function Get Axis into get axis raw. This way, we will take only strict numbers, minus one, zero or one.

Let’s improve our script more. The second tip you should know is Serialization. Making fields visible in inspector does not mean changing their modifier to public. Private fields also can be visible, if you add attribute called Serialize Field. As you see, fields are still visible in the inspector. Now I make Rigidbody invisible, I want to show you how to access components without drag and drop. There is a specific function. Go to Start method and type: rb equals Get Component Rigidbody 2D. Remember this method, it’s extremely useful.

Now it’s time to add jumping back. Let’s make it also with the usage of Input manager. In previous tutorial, we’ve added jumping at Update loop, but for physics there is a separate method – FixedUpdate. It more suitable for rigidbodies, so work with them only inside FixedUpdate.
Check if Space button pressed, type Get Button Down Jump. Then add force to the object.
Run the game. Maybe you noticed that cube jumps once, but sometimes you press the spacebar and player does not responds. Why? That’s because we check the Input inside the FixedUpdate, which is perfect for rigidbodies, but not suitable for Input Manager. So what to do next?

I’ll show a trick. At the beginning, we used Input Get Axis Raw for movement. We could use the same method instead of Button Down. Yes, the player will jump too high again, but don’t worry, we will fix that in a minute. Add float jump Input outside. Then go to Update and enter: jump Input equals to input get axis raw Jump. It will take 1 when spacebar is pressed and 0 when it released. And now just multiply force by jump input. Test the game. Now our player responses to key pressing perfectly, great!
Also maybe you`ve noticed that sometimes player rotates in wrong direction. Let’s prevent this by opening the Rigidbody 2D and checking the freeze at Z axis.

OK, and now we are ready to fix the final problem – jump only when you at the ground. Logically, we have to save a Boolean that player have already hit the ground, and set it to false when spacebar was pressed. Select Obstacle. In the top right corner of the Inspector, you can see a dropdown menu called Layer. Change layer for other floor objects, you can select them all together at Hierarchy. Excellent!
To check collisions, there is a function OnCollisionEnter2D.
Firstly, I want to check if it works. Debug Log will send a message to console. Run the game. Every time I collide with floor, it notifies me, great!

Lets launch a game, don’t forget to set enough jump force. And it works, amazing!
Also you can use more professional approach by adding the on collision exit 2D function. It is similar to OnCollisionEnter2D but called after stopping collision with object. Clear that Boolean at FixedUpdate. And this method works too, in my opinion, it keeps code cleaner, so I’ll use this one.


#unity #kotorman #unity2d #platformer #gamedev #indiegame #gamedevelopment #UnityPlatformer #2dgames #gamedesign #pixelart #unityassetstore #unitytutorials #indiedev #gameprogramming #unityengine #GameDesignTutorial #gameart #unitytips


Watch video JUMP in UNITY! | FIRST GAME | PLATFORMER 2D EASY online without registration, duration hours minute second in high quality. This video was added by user Kotorman Entertainment 11 January 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 85 once and liked it 2 people.