Тёмный

2D Player Movement In Unity 

bendux
Подписаться 13 тыс.
Просмотров 403 тыс.
50% 1

Learn how to move and jump in Unity!
Source code: gist.github.com/bendux/5fab0c...
SOCIAL
Discord: / discord
itch.io: bendux.itch.io/
Twitter: / bendux_studios
SUPPORT
Buy Me a Coffee: www.buymeacoffee.com/bendux
MUSIC
By the Fireplace by TrackTribe
Home for the Holidays by TrackTribe

Наука

Опубликовано:

 

2 фев 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 2 тыс.   
@guyinaraincoat4292
@guyinaraincoat4292 Год назад
This tutorial was very helpful by showing the whole process of making the script that made the character move, Thank you!
@fluffystuff
@fluffystuff Год назад
as a complete beginner. I did not understand this video very well but after a few weeks of messing around in unity and i can finally understand what is in this video
@Externium
@Externium Год назад
Yeah what in the world is going on here ._.
@Skitozzz
@Skitozzz Год назад
@@Externium :)
@ErraticPulse
@ErraticPulse Год назад
Fun Fact, if u look at some of the areas on the script window you will notice that the language is in german
@dumsparaside
@dumsparaside 4 месяца назад
yes I found out
@enderpigerdragon3589
@enderpigerdragon3589 9 месяцев назад
Thanks for the awesome 2D movement in Unity tutorial! Super helpful! 👍
@schauerv
@schauerv 2 года назад
God tier tutorial, finally something that is short and works great instead of an 18 min video that bugs out!
@Skitozzz
@Skitozzz Год назад
Fr fr
@The_Gronne
@The_Gronne Год назад
This is probably the best programming tutorial I've ever seen. Extremely easy and to the point. And for someone who's done this multiple times in the past, but simply don't want to type it all out again, linking the source code makes everything so much easier.
@notwasnttaken
@notwasnttaken Год назад
Mee too, i give it 100/100!
@historyjunior
@historyjunior Год назад
This is an amazing tutorial! Thank you so much, I really loved this!
@deilruchi
@deilruchi Год назад
If your character is moving left and right but doesn't jump after finishing tutorial you might have missed the point where he moves Ground Check object early in the video closer to players feet and ground. 🎉
@joshpreston4957
@joshpreston4957 Год назад
Been working on this issue for literally weeks, bless you wise internet stranger!
@deilruchi
@deilruchi Год назад
@@joshpreston4957 Glad it helped you, because it took me a while as well.
@mariazia221
@mariazia221 Год назад
where
@deilruchi
@deilruchi Год назад
@@mariazia221 ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-K1xZ-rycYY8.html
@Garfield_Minecraft
@Garfield_Minecraft Год назад
OMG thank you god it's been 3 hours just sitting here what went wrong
@mitchelllockyer7541
@mitchelllockyer7541 Год назад
bippity boopity your code is now my property
@polyhedrainteractive9663
@polyhedrainteractive9663 8 месяцев назад
This is incredible, I coded for a while but couldn't get past the basics will watch more
@yumura137
@yumura137 10 месяцев назад
very simple yet satisfying and fun movement, straight to the point video, simply one of best on youtbe
@monkeypwners
@monkeypwners 10 месяцев назад
Very nice video, easy to follow! I just feel like you go over some stuff without explanation, which can make it difficult to understand why we do what we do, like Collision Detection, Sleeping mode and interpolate. I have no idea what it means, but I just do as you say and it works :) Great video overall tho! Big props!
@lemason5981
@lemason5981 Год назад
that ground checking system is simplistic brilliance, I was stuck for hours trying to think of that and in this moment i am hoping that you're a wizard and it's not just some super simplistic thing everyone has been doing for ages except me
@bendux
@bendux Год назад
Haha, I'm definitely not a wizard.
@NewSunnahAcquired
@NewSunnahAcquired Месяц назад
@@bendux thats exactly what a wizard would say
@nagisa1578
@nagisa1578 Год назад
Man thx, did know / thought you cloud just change the velocity in script. Makes everything soooo much easyer and I dont have to fiddle around with AddForce etc. Thank you again man.
@francheeze
@francheeze 11 месяцев назад
Simple, quick & elegant. Nice!
@JustSplatoonGameplay
@JustSplatoonGameplay Год назад
Hey! Just wondering, if we want to animate, what boolean or Float should i use for the transition?
@mangmoommoommoom2000
@mangmoommoommoom2000 2 месяца назад
Yeah bro I was about to ask tha’
@LilCouve
@LilCouve Год назад
Thank you, this was my first time using C# and this really helped me!
@yugnatata
@yugnatata 2 месяца назад
Thank you! I spent over 10 hours trying to do this and you showed me how to in 5 minutes, it's perfect! Just had to be a little careful as you didn't say out loud everything you were saying, but it's otherwise an amazing video, and still up to date! Thanks again!
@Tapov_
@Tapov_ Год назад
Thanks for the tutorial! I'm a beginner and this helps alot!
@FireGloves
@FireGloves 9 месяцев назад
If anyone's wondering why they can't jump, the "jump" key is set to space by default. To change it to something else, here's the code. if (Input.GetKeyDown(KeyCode.UpArrow) && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); } if (Input.GetKeyUp(KeyCode.UpArrow) && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); }
@Bigblackhawk123
@Bigblackhawk123 8 месяцев назад
It doesn’t work even if I press space though 🫤
@dalegacy2877
@dalegacy2877 8 месяцев назад
@@Bigblackhawk123You definitely done something wrong then. I had the same issue until I linked all the objects. Try watching the video multiple times to understand what is actually going on rather than saying it doesn't work. :)
@zanahtile6276
@zanahtile6276 8 месяцев назад
Check if your Ground Check is actually "grounded". If you create it first, it places the object in the middle of the player, this causes it to never touch ground. And results in failing the ground check and blocking you from jumping. Move it lower on the player.@@Bigblackhawk123
@swiniapierdek
@swiniapierdek 7 месяцев назад
omg thank you I haven't knew that.
@TheGreyCat1026
@TheGreyCat1026 6 месяцев назад
that was the problem, thanks!@@zanahtile6276
@Skylar.H93
@Skylar.H93 Год назад
Amazing, I was able to figure it out from the video with only like 2 errors that were extremely easy to solve. Superb video! Now I just gotta figure out how to make magic fist attacks with effects, determine a combo system, and design and figure out how to link side profiles to the player sprite with walking animations.
@bendux
@bendux Год назад
I'm glad I could help. Good luck!
@LucaThePupineer
@LucaThePupineer 5 месяцев назад
HOW THIS FEELS IMPOSSIBLE
@W5AKC
@W5AKC 2 года назад
Very nice and simple.
@JohnKirk-eb6nk
@JohnKirk-eb6nk Год назад
I have watched tons of tutorials, best one yet
@BILGI_PASA
@BILGI_PASA Год назад
Thank you bendux. The video helped me so much.
@slayerghande7969
@slayerghande7969 2 года назад
Yo, great work here 👏. Don't ever stop and you will be the new brackeys
@frogwav
@frogwav Год назад
You explained this well. also it works. thanks!
@foo3268
@foo3268 2 года назад
underrated channel, hopefully unity notices you someday
@lennystudios3.14
@lennystudios3.14 8 месяцев назад
Love it, I personally like adding a Lerp function for some smoother acceleration and floatier physics, but that’s just my prefrance
@amburdo6206
@amburdo6206 Год назад
Thanks for the great video. I wanted to ask about one thing I'm quite confused about. It seems that you didn't program any horizontal movement keys that need to be pressed down to move, yet you still are able to move. The only key you referenced in the script was jump. How is that possible?
@bendux
@bendux Год назад
Edit > Project Settings > Input Manager
@amburdo6206
@amburdo6206 Год назад
@@bendux Thanks a lot!
@lilili765
@lilili765 8 месяцев назад
​@@benduxwhat do i do in inputmanager
@matsu4721
@matsu4721 2 года назад
Thanks for this awesome smooth moving video, but can you do 2d enemy ui or shooting?
@bendux
@bendux 2 года назад
I've added it to my list. Thank you for the suggestion!
@ZephyrAnimationss
@ZephyrAnimationss 2 года назад
@@bendux YEsssssss exactly what i wanted thanks(i want to make a shooter game cuz im new)
@Jamal38852
@Jamal38852 Год назад
You literally saved me by making this video, earned a sub
@DevNoob
@DevNoob 2 года назад
Always good videos! 1k subs let's go!
@Arrzee.
@Arrzee. 11 месяцев назад
Here is the script for those who need it using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovement : MonoBehaviour { private float horizontal; private float speed = 0f; private float jumpingPower = 16f; private bool isFacingRight = true; [SerializeField] private Rigidbody2D rb; [SerializeField] private Transform groundCheck; [SerializeField] private LayerMask groundLayer; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { horizontal = Input.GetAxisRaw("Horizontal"); if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); } if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); } Flip(); } private void FixedUpdate() { rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); } private bool IsGrounded() { return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); } private void Flip() { if ((isFacingRight && horizontal < 0f) || (!isFacingRight && horizontal > 0f)) { isFacingRight = !isFacingRight; Vector3 localScale = transform.localScale; localScale.x *= -1f; transform.localScale = localScale; } } }
@wizard7317
@wizard7317 11 месяцев назад
yo, my serialized fields arent showing up in my unity project and i was wondering if you could help
@noona7701
@noona7701 10 месяцев назад
@@wizard7317 did u save the script in vs before going back to unity?
@furyz0339
@furyz0339 9 месяцев назад
this script gives me no movements :c i turn left and right but cannot move
@noona7701
@noona7701 9 месяцев назад
@@furyz0339 you have to change the speed in vscode to 8 (in the comment it is set to 0)
@furyz0339
@furyz0339 9 месяцев назад
thank youuu @@noona7701
@its_a_gamer5279
@its_a_gamer5279 Год назад
For everyone that struggles with the player falling through the ground, Make sure that your ground is not imported from a file but made in the 'Hierarchy'. (if its not imported its grey in the hierarchy otherwise its blue) Good luck!
@bendux
@bendux Год назад
Thank you for sharing!
@its_a_gamer5279
@its_a_gamer5279 Год назад
@@bendux np!
@wogs2k
@wogs2k Год назад
I did this and it still doesn't work
@bendux
@bendux Год назад
@@wogs2k Join our Discord server, and let's solve your problem together!
@sirnoodle2004
@sirnoodle2004 Год назад
@@wogs2k Also make sure that on both the BoxCollider2D of the player and the platforms, the box "Is Trigger" is not ticked
@markotafa
@markotafa 6 месяцев назад
I like people that actually put code in the despcription :)
@potato_legend127
@potato_legend127 Год назад
Thank you for this video. It helped me a lot!!!
@alexwellman6605
@alexwellman6605 Год назад
I have a problem with the movement, I added the no friction material to two objects, but after they collide, they start getting stuck on the floor, even after moving and jumping The floor uses tilemap colliders, if that helps Edit: probably solved, I used a composite collider too
@bendux
@bendux Год назад
Add a Composite Collider 2D component to your tilemap.
@alexwellman6605
@alexwellman6605 Год назад
@@bendux thank you so much
@feddyfadbear7985
@feddyfadbear7985 2 года назад
hey, great video, everything is working fine but im jumping WAY too high, how can i decrease the jump power?
@bendux
@bendux 2 года назад
Decrease the jumping power at the top of the script.
@BHArv3y
@BHArv3y Год назад
you can change the value in the script from "private float jumpingPower" to "public float jumpingPower" and this will allow you to directly adjust the power in unity rather than opening the script! :D good luck!
@gameormusic2360
@gameormusic2360 Год назад
bro is definitely not a beginner, Very helpful video and thank you
@tunahanyilmazdev
@tunahanyilmazdev 2 года назад
Great as always!
@Nintendostar02
@Nintendostar02 Год назад
When you'll make a video about how do object follow the player (like Mario, luigi and paper mario in Mario and luigi paper jam), i'll be very happy. Great video, i resolved all my problem with 2d player movement
@bendux
@bendux Год назад
I've added it to my list. Thank you for the suggestion!
@krzaczastyyt6547
@krzaczastyyt6547 2 года назад
Your tutorials are the best i gona make a game with those
@eshaanb2362
@eshaanb2362 29 дней назад
Thank you so much, this tutorial was very helpful!
@KenB1206
@KenB1206 11 месяцев назад
thank you so much, sir bendux! it help quite significantly
@cheezeburger7716
@cheezeburger7716 Год назад
Very nice tutorial, one thing that I noticed, or if I may have missed, however, is that when I go to jump, the player doesn't have a limit to how many times they can jump in the air. Is there something that I would need to type in to fix that? I noticed that wasn't established in the video, but if anyone has an idea of how to put that in it would mean a lot. I'm a beginner at coding myself, so I'm not too big on this kind of stuff.
@bendux
@bendux Год назад
Would you like to show me your code on Discord?
@cheezeburger7716
@cheezeburger7716 Год назад
@@bendux Oh, thank you. But I actually managed to figure it out from another source. I appreciate the offer though. And what all you had shown here in the video.
@bendux
@bendux Год назад
@@cheezeburger7716 I'm glad you were able to fix it.
@ab7299
@ab7299 Год назад
@@cheezeburger7716 can you tell me how you fixed it? i want to fix it too
@aniime4006
@aniime4006 Год назад
@@cheezeburger7716 can you tell me how you fixed it? i want to fix it too
@EmirDuman1
@EmirDuman1 5 месяцев назад
i can't jump
@finngrainger349
@finngrainger349 5 месяцев назад
neither, left and right works just no jump
@wayvoedorado71
@wayvoedorado71 4 месяца назад
have any of you found a solution yet? It looks like it's a common problem.
@finngrainger349
@finngrainger349 4 месяца назад
yes, my problem might be different to yours but I solved it by moving the 'ground check' to the position of the bottom of the player as I forgot to move it as said at 1:17@@wayvoedorado71
@wayvoedorado71
@wayvoedorado71 4 месяца назад
@@finngrainger349 that wasn't my problem, but you encouraged me to look into it and my problem was that I forgot to change the platform layer to ground so thank you
@bendux
@bendux 4 месяца назад
You probably skipped one or two important steps in the tutorial.
@artofarii
@artofarii Год назад
Nicely Explained. Thank you so much!
@r.ssolanki4841
@r.ssolanki4841 Год назад
Very well explained, thanks a lot!!
@RichardBarnes0
@RichardBarnes0 2 года назад
The old input system is not worth using anymore.
@lilDrizzyyy
@lilDrizzyyy Год назад
Yes it is stfu
@VonMoosHarrison
@VonMoosHarrison Год назад
Literaly the most difficult to understand tutorial
@Think_Stream
@Think_Stream Год назад
thanks a lot bro i went to many videos just for this even brackys tutorial didnt work and i tried this love you bro
@iAmStanee
@iAmStanee Год назад
Brackys tutorial is where all the souls go😂😂
@travisc8406
@travisc8406 Год назад
can we just appreciate that he is still responding to comments! you're awesome!
@1ts_Leen0
@1ts_Leen0 6 дней назад
I struggled with the built-in input manager. No matter what I did it seemed to apply some form of deceleration/acceleration when I moved the player. So instead, I did something similar to you in this video and that fixed it. Nice and snappy movement, just as you'd expect in a 2D game.
@everything-editz785
@everything-editz785 Год назад
Best unity tutorial i havrme ever seen 10/10
@vrsuhfx
@vrsuhfx 2 года назад
Hey! Quick question, how exactly would i make the run/walk a method if possible? Just so that I have a place to add in my audio, dust particles, etc. Cuz I'm new to this whole thing haha
@bendux
@bendux 2 года назад
The answer to your question goes beyond the scope of a RU-vid comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
@xsool-yl7us
@xsool-yl7us Год назад
this help out a ton thanks man
@kruziksuleyk9536
@kruziksuleyk9536 2 часа назад
This helped heaps thank you so much
@dmitriyleonov6977
@dmitriyleonov6977 6 месяцев назад
Отлично. Я делал подобный урок от другого автора. Персонаж дёргался. А тут всё движется плавно и код выполнен здорово компактно. Настройки физического тела показал как настроить . Спасибо за урок
@eylul2140
@eylul2140 Год назад
Your tutorial is great ♥
@Hollowdude15
@Hollowdude15 Год назад
Unity is so cool and nice video man :]
@woslake7576
@woslake7576 Год назад
Thanks !! I love your video.
@renegade1807
@renegade1807 Год назад
Thanks so much. Had a lot of trouble with this issue. Sub'd
@Nullphode
@Nullphode 6 дней назад
this'll come in handy
@Bashi_boi
@Bashi_boi 3 месяца назад
Really helpful tutorial thanks :)
@Hyper_alexa
@Hyper_alexa Год назад
that is helping me a lot man tnx
@felixkostur723
@felixkostur723 11 месяцев назад
thx this was really useful and it works for me
@RelaxReisen
@RelaxReisen Год назад
Danke, hat problemlos geklappt!
@kidthelett
@kidthelett 3 месяца назад
Beautiful. Kinda funny I thought the jump didn't work cause i was pressing w or the up button, but then i realized it was space
@Nexorix_
@Nexorix_ Год назад
Am a noob in c sharp and it worked intantly Thanks man !
@jetsi604
@jetsi604 Год назад
THANK YOU! I was strugling with my code for couple hours :D
@jonasgustafsson1032
@jonasgustafsson1032 10 месяцев назад
Very Good tutorial ❤
@puyanph6114
@puyanph6114 Год назад
Nice job bro 😀❤️
@Apleker
@Apleker Год назад
Hippity Hoppity Your Code Is Now My Property - "Apleker"
@deanv1265
@deanv1265 2 года назад
Best youtube channel in town
@gencerlive
@gencerlive 11 месяцев назад
Thanks bro helps alot
@carlosalbertobatistacruz
@carlosalbertobatistacruz 7 месяцев назад
Nice tutorial, the SpriteRenderer Component can also be used, just get the flipX (bool) attribute, instead of using localScale -1.
@cheese_master01
@cheese_master01 2 месяца назад
helped a lot! 👌
@dennisestepro
@dennisestepro Год назад
Hey! I am wondering how can I stop the player from moving when he hits the edge of the screen? If I just add an if to check the transform.position.x is lower or higher than the value of the edge of the screen, the player just keeps on going without me touching any buttons. Any opinions?
@bendux
@bendux Год назад
You could create an empty game object and add a Box Collider 2D component to it.
@BarrowWight
@BarrowWight Год назад
Which button input is used to jump? I know this is a dumb question but I followed the code exactly and tried all buttons on my keyboard but not triggered any sort of jumping action.
@bendux
@bendux Год назад
Did you position the ground check at your player's feet?
@BarrowWight
@BarrowWight Год назад
Hadn't checked that. thanks for the advice :)
@excaliber_bruh
@excaliber_bruh Год назад
thank you so much man also my code was giving me a million errors so i just downloaded code and now it works perfectly 😎
@bendux
@bendux Год назад
I'm glad you were able to fix it.
@arda7z657
@arda7z657 2 года назад
Understandable and easy.
@shinsane1558
@shinsane1558 Год назад
Hey! i got a problem, its saying this in the console: Assets\PlayerMovement.cs(13,30): error CS0246: The type or namespace name 'RigidBody2D' could not be found (are you missing a using directive or an assembly reference?)
@bendux
@bendux Год назад
Try Rigidbody2D instead of RigidBody2D.
@dkoorse
@dkoorse Год назад
thanku! its a good movement script
@neelrakesh2623
@neelrakesh2623 7 месяцев назад
Really helpful!
@Jamsey200
@Jamsey200 11 месяцев назад
Thanks for teaching us in dark mode
@emilian_spielt300
@emilian_spielt300 Год назад
Thanks man! i had some struggle with the code but after i "Rebuild" it, it worked really nice for me! thanks for you video
@-._122
@-._122 11 месяцев назад
@bendux THIS IS THE BEST TUTORIAL EVER!!! I CAN MOVE AND JUMP btw you just earned a sub
@bendux
@bendux 11 месяцев назад
Thank you!
@-._122
@-._122 10 месяцев назад
@@bendux no problem
@RedLeicesterCheese
@RedLeicesterCheese Год назад
Very nice tutorial
@loawkkx
@loawkkx Год назад
With this configuration, do you know how I can affect the velocity in another way because it is defined at each frame? I can't affect the velocity in any other script at all
@bendux
@bendux Год назад
The answer to your question goes beyond the scope of a RU-vid comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
@atauygur8104
@atauygur8104 8 месяцев назад
I understand that if the movement button is pressed down continuously there will be no errors, but doesn't this code have a possibility to miss input when we just pressed for a small time? Because, there is nothing to prevent "horizontal" variable to be updated multiple times between two consecutive "FixedUpdates".
@funforgingstudio4690
@funforgingstudio4690 2 года назад
Thank you very much!
@Sooss_und_Zoouro
@Sooss_und_Zoouro Год назад
Jo Gleises Video Bruder :D
@chrono9503
@chrono9503 11 месяцев назад
is it possible to assign the properties of the "jump" button to a ui button? I want this code to work with my mobile game
@c0rse198
@c0rse198 Год назад
After 3 hours of attempting to figure out why I wasn't able to jump, I found that you need to assign your keys to the "Jump" input in input settings. While horizontal is pre-set, jump is left blank and this left me absolutely baffled until I figured it out.
@debramciver307
@debramciver307 Год назад
HELP PLEASE
@IvanLira
@IvanLira Год назад
THANK YOU SO MUCH !
@ukasz4848
@ukasz4848 16 дней назад
Hi, would't this solution allow jumping mid-air after falling from a platform?
@mrcodingfrog8937
@mrcodingfrog8937 2 года назад
Thank you so much! :DDDD
@user-tx9kr8mz1v
@user-tx9kr8mz1v Год назад
Thanks. Realy helped
@beepboop2851
@beepboop2851 Год назад
Btw if u are having problems with the rub, ground check and ground layer not appearing then just copy and paste the source code in the description because I had followed all the steps and it wasn’t working but when I copied it the errors got fixed
@bendux
@bendux Год назад
The source code on GitHub is the same as in the video.
@antoniosalazar9505
@antoniosalazar9505 Год назад
Thanks, I was just looking for this.
@Prod_Joel-beats
@Prod_Joel-beats 10 месяцев назад
What buttons do you move left and right on? I can jump but idk how to love left and right
@bendux
@bendux 10 месяцев назад
Edit > Project Settings > Input Manager
Далее
How To Dash In Unity
3:53
Просмотров 129 тыс.
TOP DOWN MOVEMENT in Unity!
22:30
Просмотров 1,2 млн
Кошка-ГОНЧАР #шортс #shorts
00:28
Просмотров 324 тыс.
2D Movement in Unity (Tutorial)
21:09
Просмотров 3,1 млн
I Beat a Game That Lasts Forever
9:03
Просмотров 85 тыс.
How much MONEY my Game made (and how much it cost!)
14:00
3 Hours vs. 3 Years of Blender
17:44
Просмотров 3,7 млн
Coding Adventure: Chess
29:22
Просмотров 3,7 млн