Code Class - 2D Player Movement in Unity
ฝัง
- เผยแพร่เมื่อ 4 ก.ค. 2024
- (reuploaded to fix an error in the edit of the previous version)
Heya Pals!
Welcome to a first in a series of video tutorials for Unity Development. We'll be covering all kinds of content in the series, so be sure to subscribe and check the playlist for future videos.
Music: Rifti Beats - Chocobo & Chill [Gamechops.com]
Chapters:
0:00 - Intro
1:00 - GameObject Setup
4:00 - Simple Top-down Movement
10:50 - Preparing your Project for Art Assets
12:38 - Sidescrolling Movement with Basic Physics
14:51 - Friction and FixedUpdate
16:41 - Ground Check
20:27 - Platforming, Acceleration and Jumping
25:55 - Summary
26:37 - Outro
27:03 - Post-Credits Code Review
----
Become a Patron at / adamcyounis
Twitch: / adamcyounis
Twitter: / adamcyounis
Tiktok: / adamcyounis
Discord: / discord
Download assets and games from the stream at uppon-hill.itch.io/
Later, pals! - วิทยาศาสตร์และเทคโนโลยี
Thank you for doing a series like this. I actually really appreciate how you explain everything, and the pacing is very good imo.
I definitely came to your channel for your pixel art content, but I am 100 percent on board for more of these unity videos!
It's pretty amazing how quickly you turned this around for how high-quality of a video this is! Thank you so much - looking forward to the rest of the series. :)
You have a gift for teaching. It comes so easy to listen and follow.
TIL that Adam is not a 'never nester.' As a more intermediate dev I really enjoyed this. I like the way you handle drag and friction through code rather than solely with the inhouse physics engine. It was refreshing to have more of a deep dive into base movement, rather than trying to fit as many 'features' into the video like double jumps and coyote time.
I feel like the nesting helps me appreciate that there's a condition there. Indentation exists for a reason, and I don't see the harm in one extra tab.
Though sometimes I make an exception if the function is particularly complex with many steps and lots of return cases. The early return can be considered step one.
YEEEEEEEAH I'VE BEEN WAITING THIS KIND OF TUTORIALS FROM YOU! THANKS!
Finally managed to catch one of your livestreams this morning and now seeing this video, perfect timing because I wanted to start unity today! All of your stuff is incredibly inspiring, and I've been wanting to dive into things myself. And while I have experience in front-end coding, most tutorials I've encountered skip the stuff that makes you actually *understand* it. Thank you Adam!
Great video! I love your dev journals, but I really truly enjoy your instructional videos for Unity and/or pixel art. Thanks!
I was looking forward this one. Thank you so much Adam. We really appreciate you bro !
I literally was searching for this and I saw your upload. Bless you
This was soo helpful, please continue this series!
As a casual user of Unity, this video really helps me to see good usages of Unity. It conforts me on some way I'm going things but also shows me features I'm didn't know about or used without really get the purpose of it. You show really really good practices on the fly where most of the tuto online are just g straight to the point but in a super dirty way. Without considering scalability, code refactoring or real implementations.
So thanks!
Great tutorial, I wish I had these style tutorials 5 years when I first began my gamedev journey! I love that you explain each of the various Unity fields & code inputs rather than just glossing over them! Thanks Adam!
Adam, your coding tutorials are a game-changer! Your teaching style is spot-on, making complex concepts easy to grasp. I'm hooked and genuinely hope you keep the series going. Your gift for teaching is making a real impact, and I'm excited to learn more. Cheers!
Just the info I was looking for, and very well explained. More like this please 🙏
The detailed explanations are awesome :D
Works great, thank you ! It's really nice to have someone covering both coding, art and game design on the same channel as i'm interested in both subjects.
In addition, your videos are of high quality and you are very pleasant to listen to in the way you explain things. Good luck with Insignia by the way, can't wait to buy it :)
Nice, would love to hear more in the later vids
these are incredibly useful and understandable, you're really good at explaining things
Thanks!
I love to see the explanations on the coded version of friction and acceleration. Awesome tutorial and well explained
Love the video! As someone who loves your videos but is working on a 3d game it's great to get some variety from pure pixel art tutorials
Thank you . Your video is better than most of the others and it helped me.
This is awesome! Exactly what i was looking for!
Yesss, i need this so much! Thank youu
Thank you so much Adam for making this. As a newcomer to making video games, this is incredibly helpful. I am really interested in more top down pixel art game development videos. I findd a wealth of content for platformer games but for top down, sadly, not so much. Keep up the amazing content and I look forward to future videos. Best of regards man.
Thank u very much, this will help alot. I'll start watching these videos a couple of months later but thanks a ton.
Just discovered this series and it’s exactly what I needed. Great content from what I’ve watched so far!
Thank you Adam.
This is gold, thanks!
Thanks for the video!
Best guide for 2d
my gosh thank you!
Gotta say, this is a lot better than the code ChatGPT wrote for me. Ai coding has a long way to go, still.
Great video.
That section at the end: I’m often bumping into where to do the check test itself (i.e. if the input has anything meaningful to do any logic)… so sometimes I end up doing it twice if I wanted to use that “CheckInput” function in a larger and more expensive method, so, I would do “if there’s input, do x/y/z/w/u/p things, else skip”, but CheckInput itself still checks if there’s any input.
Could return the results of the first check input boolean but then it starts getting over-engineered quick…
FINALLY
awesome!
My only gripe with the way that you've set up the groundCheck collider, is that if your character's feet hit the side of a platform, it will count it as being grounded, which it obviously isn't. The way I usually get around this is by creating a ground detection collider within the player collider that's only slight smaller on the top, right, and left sides, but matches the bottom side, such as that only the bottom of the character will detect ground and not the lower sides. I've yet to run into any issues with this method, but it could definitely make slopes challenging to deal with.
Edit: I take it back, I see that a few moments later you set the groundCollider to only consider overlaps rather than simple collisions, which would definitely solve the issue I mentioned, but I'll still keep my comment for posterity in case anyone had similar concerns. That said, I have seen that it's possible to set a Contact Filters, where you can specify a min and max angle to be considered when checking ground collisions in such a way that slopes that exceed the specified angles do no count as being grounded? Any experience with those?
This problem can be solved a couple of ways depending on how complex the terrain and player behaviour need to be. There is always going to be a catch 22. if the ground check is any narrower than the body collider, it'll be possible to be standing right on the edge but not "grounded". If it's wider, "grounded" will be true when touching walls marked as ground. Neither are desirable, but I prefer to solve the latter with additional logic, or a better terrain setup that separates walls and ground more precisely.
It would be cool to see a video on reusable code by you. using pseudo code so it's easier to implement in other engines might be a good idea too
Could you do more isometric tutorials? I'm struggling with my isometric RTS game, particulary with VFX and weather
I know you debated godot on your live stream, but still would love these to be also for godot.
hey Adam, just wondering what you think of the new input system? would you recomend?
Yes, I think it's worth learning. The old input system is fine for simple games but anything with local multiplayer and support for different controllers should use the new input system, or a third party plugin like Rewired.
Is it me or the jump heigh is lower if player is not moving?
What editor theme are you using here? Really like the colors.
This is the Material theme, set to "Ocean"
sry for so many questions but was wandering when i take damage i get dealt double the damage in some spots cuz of the 2 colliders is there a way no to?
Great Tutorial. Some things sadly still go a bit over my head, but I understood a good bit.
One problem I am having right now in my project is that after implementing the whole video my character keeps sliding a little after I let go of the input key. I assumed that I could fix this with the "ground decay" value to add more drag, but I already put it to the lowest it can be (at 0) and my character still slides a good bit after I let go of the button.
Does anyone know how I could bring the player character to a more abrupt stop?
A bit outside the scope of this video, but a discussion/debate I've seen over the years is using the built in Unity physics vs making your own physics system for a platformer.
Obviously, in the video you use the former, but was still wondering if you have any thoughts on that?
I haven't explored this topic enough to say whether writing a physics system for a precision platformer mechanics from scratch would be more time consuming than modifying Unity's physics. Things like the contact solver are actually not trivial to implement, and would be significant work, unless the game doesn't need those kinds of features.
Why do you multiply the groundDecay in the y Axis? This in the ApplyFriction method.
Hi I’m struggling to understand what’s the “correct” or standard approach to dealing with ground checks and player hitboxes when their sprite changes in shape. Another thing I’m wondering about is how do I go about actually building a level? I understand putting down my sprites as gameobjects or using a tilemap but where I’m unsure is properly building the colliders. My current approach is to use a tilemap collider 2D + a composite collider2D but it just feels hacky
Ideally, your character's physics collider / ground check should never change, even if the sprite does. It's much much easier to manage this way and players will not care about the inconsistencies in the sprite silhouette vs physics shape.
As for hit boxes and level colliders, I'll cover them in later videos.
Thank you, looking forward to those vids. Oh one thing I forgot to mention, I'm not going for a pixel art style but rather a hand drawn game. Are there any technical differences I need to consider in relation to physics or level design?
When I move my character in a side scroller type of game, I slide and accelerate when I'm at 2:25 in the video, if that makes sense
i can't get Vector2 direction to work
in the part at 9:10 it keeps giving me the Assets\playermovement.cs(25,34): error CS1513: } expected error and i dont know what it is talking about some one please help
Man, I want to make my own game, but I don't know how to begin learning coding. Do you have some advice? How did you begin learning coding?
Bro I know u have made video on character size and canvas size but we want to know the exact size of ur game characters and ur canvas size, map size, tileset size, etc
Do you delete your youtube vods older than a month? wanted to go back and watch the LD godot streams and only lives on there are no longer than a month old :(
I wasn't streaming on TH-cam before a month ago! Twitch has always been my primary platform.
Crouch tutorial please
i keep getting a syntax error on the vector 2 part, idk why someone help please
why doesn't unity recognise "direction" when coding the movement speed?
Guy's can someone help me, I was trying to follow along but then an error popped up even though I did everything the guy said to do (The error popped up at the part where you start coding)
nothing that says "body" shows up here 4:29
when i jump it doesn't jump the full height (it would do a tiny hop) unless i hold the jump button is there a way to fix this?
In the video I get around this by only applying friction with a surface if your velocity.y is
Have you resolved the problem? I'm experiencing the same issue.
@@RakeBye22 i have i forgot to remove the comment but it was 2 days after i commented and i forgot how i fixed it im pretty sure i typed something in wrong so its good now again sorry
@@AdamCYounis sorry for this its not really a problem but i was wandering is there a way to get rid of the ground slide. whenever i click the button for a second it and stop either immediately or after alittle bit it slides a set distance i tried adjusting the gound decay, max X speed, but it doesnt seem to matter
@@Borgor_Eater if you don't want to use any friction, you can just set the velocity.x to 0 if the character is grounded and there are no horizontal keys being pressed.
21:13 how am I supposed to follow this
my gravity is lower or moves slower down then yours with linear drag on 10 i float down like in honey idk why my linear drag is 0
I had to watch at 0.75x
at 7:06 he changes the code without telling anything, i got tricked by this lol
I don't understand anything.
weren't you moving away from Unity?
It's the engine I have the most experience in, (I'm still making my passion project in it) and the place I can share the most value as a content creator, given that the audience is still greater for Unity than any other engine.
The decision was not made lightly, of course. I am still excited to explore other engines in my free time, particularly Godot which I enjoyed experimenting with last year. Maybe when I have more experience there I can create tutorials for it, too.
Shame that this series won't help me, since the 'runtime fee' stunt made me lose any possible interest in Unity.