2D Player Movement In Unity
Вставка
- Опубліковано 2 лют 2022
- 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 - Наука та технологія
This tutorial was very helpful by showing the whole process of making the script that made the character move, Thank you!
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
Yeah what in the world is going on here ._.
@@Externium :)
Fun Fact, if u look at some of the areas on the script window you will notice that the language is in german
yes I found out
Jaja das hast du gut gesehen
@@CoderMan5000 shut up
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. 🎉
Been working on this issue for literally weeks, bless you wise internet stranger!
@@joshpreston4957 Glad it helped you, because it took me a while as well.
where
@@mariazia221 ua-cam.com/video/K1xZ-rycYY8/v-deo.html
OMG thank you god it's been 3 hours just sitting here what went wrong
Thanks for the awesome 2D movement in Unity tutorial! Super helpful! 👍
This is an amazing tutorial! Thank you so much, I really loved this!
bippity boopity your code is now my property
God tier tutorial, finally something that is short and works great instead of an 18 min video that bugs out!
Fr fr
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);
}
It doesn’t work even if I press space though 🫤
@@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. :)
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
omg thank you I haven't knew that.
that was the problem, thanks!@@zanahtile6276
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!
This is the only tutorial where I get good physics for the player, thanks so much :)
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.
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.
Mee too, i give it 100/100!
Thank you, this was my first time using C# and this really helped me!
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!
This is incredible, I coded for a while but couldn't get past the basics will watch more
Hey! Just wondering, if we want to animate, what boolean or Float should i use for the transition?
Yeah bro I was about to ask tha’
Yo, great work here 👏.
Don't ever stop and you will be the new brackeys
very simple yet satisfying and fun movement, straight to the point video, simply one of best on youtbe
Thanks for the tutorial! I'm a beginner and this helps alot!
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
Haha, I'm definitely not a wizard.
@@bendux thats exactly what a wizard would say
Very nice and simple.
Simple, quick & elegant. Nice!
Love it, I personally like adding a Lerp function for some smoother acceleration and floatier physics, but that’s just my prefrance
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.
I'm glad I could help. Good luck!
HOW THIS FEELS IMPOSSIBLE
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
The answer to your question goes beyond the scope of a UA-cam comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
I have watched tons of tutorials, best one yet
Thank you bendux. The video helped me so much.
Thanks for this awesome smooth moving video, but can you do 2d enemy ui or shooting?
I've added it to my list. Thank you for the suggestion!
@@bendux YEsssssss exactly what i wanted thanks(i want to make a shooter game cuz im new)
You explained this well. also it works. thanks!
Thank you for this video. It helped me a lot!!!
thank you so much, sir bendux! it help quite significantly
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?
Edit > Project Settings > Input Manager
@@bendux Thanks a lot!
@@benduxwhat do i do in inputmanager
The video would be a lot better if you'd actually explain WHY you do certain things instead of just telling us THAT you're doing it. Could have just provided the source code without a video at this point (for the most parts).
this is the biggest mistake online unity tutorials make. they never explain why things work the way they do.
Exactly!
You literally saved me by making this video, earned a sub
Always good videos! 1k subs let's go!
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
I've added it to my list. Thank you for the suggestion!
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!
Thank you for sharing!
@@bendux np!
I did this and it still doesn't work
@@wogs2k Join our Discord server, and let's solve your problem together!
@@wogs2k Also make sure that on both the BoxCollider2D of the player and the platforms, the box "Is Trigger" is not ticked
Отлично. Я делал подобный урок от другого автора. Персонаж дёргался. А тут всё движется плавно и код выполнен здорово компактно. Настройки физического тела показал как настроить . Спасибо за урок
Very well explained, thanks a lot!!
Your tutorials are the best i gona make a game with those
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.
Would you like to show me your code on Discord?
@@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.
@@cheezeburger7716 I'm glad you were able to fix it.
@@cheezeburger7716 can you tell me how you fixed it? i want to fix it too
@@cheezeburger7716 can you tell me how you fixed it? i want to fix it too
thanks a lot bro i went to many videos just for this even brackys tutorial didnt work
and i tried this love you bro
Brackys tutorial is where all the souls go😂😂
@@iAmStanee its just really outdated
Nicely Explained. Thank you so much!
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
Add a Composite Collider 2D component to your tilemap.
@@bendux thank you so much
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;
}
}
}
yo, my serialized fields arent showing up in my unity project and i was wondering if you could help
@@wizard7317 did u save the script in vs before going back to unity?
this script gives me no movements :c i turn left and right but cannot move
@@furyz0339 you have to change the speed in vscode to 8 (in the comment it is set to 0)
thank youuu @@noona7701
underrated channel, hopefully unity notices you someday
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.
hey, great video, everything is working fine but im jumping WAY too high, how can i decrease the jump power?
Decrease the jumping power at the top of the script.
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!
@@bendux didnt do anything, is there another fix?
@@gumball5372 Increase the gravity scale of your player's Rigidbody 2D component.
@@bendux i found the fix, i tried this but it didnt work. I have no clue what the problem was but it works now
i can't jump
neither, left and right works just no jump
have any of you found a solution yet? It looks like it's a common problem.
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
@@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
You probably skipped one or two important steps in the tutorial.
I like people that actually put code in the despcription :)
bro is definitely not a beginner, Very helpful video and thank you
The old input system is not worth using anymore.
Yes it is stfu
Literaly the most difficult to understand tutorial
Thank you so much, this tutorial was very helpful!
this help out a ton thanks man
This helped heaps thank you so much
thank you so much ,without you i would still be stuck in how to move it .
you've got a sub
can we just appreciate that he is still responding to comments! you're awesome!
Great as always!
Best unity tutorial i havrme ever seen 10/10
that is helping me a lot man tnx
Nice tutorial, the SpriteRenderer Component can also be used, just get the flipX (bool) attribute, instead of using localScale -1.
Your tutorial is great ♥
thx this was really useful and it works for me
Thanks !! I love your video.
Thanks so much. Had a lot of trouble with this issue. Sub'd
THANK YOU! I was strugling with my code for couple hours :D
Am a noob in c sharp and it worked intantly Thanks man !
thank you so much man also my code was giving me a million errors so i just downloaded code and now it works perfectly 😎
I'm glad you were able to fix it.
@bendux THIS IS THE BEST TUTORIAL EVER!!! I CAN MOVE AND JUMP btw you just earned a sub
Thank you!
@@bendux no problem
Unity is so cool and nice video man :]
Really helpful tutorial thanks :)
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
Thanks bro helps alot
Very Good tutorial
❤
Nice job bro 😀❤️
Danke, hat problemlos geklappt!
This helped me alot and I even somehow got my player to infinite jump like flappy bird. Definitally a win
If you want to get rid of this behavior in the future, make sure that your player and the ground are not on the same layer.
@@bendux Thx man, oh and btw I subbed to you for your awesome Unity tips!!
@@KrixerVR I appreciate it. Thank you!
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.
HELP
PLEASE
Thank you very much!
I really appreciate to you that created this video cause it's helpful for beginners like me
Hippity Hoppity Your Code Is Now My Property - "Apleker"
this'll come in handy
Best youtube channel in town
Thanks man! i had some struggle with the code but after i "Rebuild" it, it worked really nice for me! thanks for you video
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
The source code on GitHub is the same as in the video.
Thanks, I was just looking for this.
I want my cube when the cube is jumping while moving to the right, the cube will jump and rotate 90 degrees to the right as well and there should be a kind of animation and not just instantly changed to 90 degrees.
Edit: just like in Geometry Dash
Very nice tutorial
Very good tutorial
Its better to use a trigger colider instead of an empty object to check for grounded so you can change the width
You can adjust the radius of the ground check.
I've spent afew hours trying to find one that worked... a 5 minute simple video, that is simple and easy to understand; compared to a 30 minute jumbled video that doesnt even work... That red subscribe button is calling!! Thank you so much
For those wondering why you can't jump remember to change the layer of your platforms to ground, had me confused for a few minutes
Thanks for teaching us in dark mode
Thank you so much! :DDDD
Understandable and easy.
Your content is amazing. Could you upload a tutorial with jump for Top-Down games, like Zelda Link's Awakening please?? :DD
Thanks. Realy helped
helped a lot! 👌
Really helpful!