When you add drag to a rigid body this effects both dimensions in Unity2D. As in rb.drag is altering our jump height. If you stay still (x direction) while jumping your jump is dampened. When you are moving while jumping it is not so the jump is higher. I changed the application of rb.drag to call a method instead. private void ApplyHorizontalDrag() { rb.velocity = new Vector2(rb.velocity.x * Mathf.Clamp01(1 - dragConstant), rb.velocity.y); } This applies the drag only in the x direction leaving the jump unchanged. I replaced the rb.drag = linearDrag with a call for this method in fixed update. The else part of the statement is not needed since this is applied each time and does not change the drag and therefore does not need to be changed back. I used dragConstant = .9f so far but lowering it gives you a softer stop when turning and such.
This kind of ground detection works fine for fast and easy prototypes. The problem with this one is obviously when u stand on an edge u will be in the falling state without falling. Sure u can add 2 more RayCasts to be safe for the most parts but if you want it 100% accurate u should sweep ur component. Unity provides u with a Box, Speher, Capsul ... Casts. That should work great for "most" cases
The jumping works fine for me, but when the player reaches the ground, the gravity scale snaps from 5 to 0 quicky, where it stops the player for a millisecond and then walks normally. Any fix to this?
Hello everyone ! does anyone knows why my character loses X velocity when landing ? Except from this problem, it all works pretty good. Thank you for the videos !
@@uba754 Nice! But yeah the physics can be really confusing, and sometimes it's hard to know where to look for a solution, or even tell what the problem is exactly.
For the ground check it is better to use a sphere cast and not a line cast Also for the short jump you can just put Y velocity to 0 when you release the jump button so the character will fall down as soon as you let the key
God, you're really teaching us how to do things profissionally. Really liked your videos and the way you teach. Became sad when I saw the last video you uploaded was 2 years ago. You haven't even finished this 4 parts tutorial ;(. Where are you?
Great video! One issue I've encountered however is that on impact to the ground, the character dips into the ground ever so slightly before popping back out and interrupts the player's velocity. I'm currently using a box collider for my character but also tried an edge collider like how you have it on yours to an even worse effect. Is there a fix to this?
Hey friend, I'm probably too late to be totally helpful HOWEVER... I've found that the player is going to dip into the ground no matter what, but going to your RigidBody 2d settings and changing your Collision Detection to continuous should help with it to some degree
a problem with your set up is that vertical is only detected to activate the falling animation when you press down the S key. but why would the player be holding down the S key just so we can see the animation play. The fall animation should play when you are falling without having to hold down the S key.
So I copied the script, I noticed that when I move horizontally then jump the character jumps higher than when it jumps while standing still, I tried several methods like clamping y velocity, using a targeted height and stop and it and others, none of them gave me that elegant result, any idea how can I fix this?
i copied the code from the description and got error Parameter 'onGround' does not exist. UnityEngine.Animator:SetBool (string,bool) leshen.PlayerController:Update () (at Assets/_Scripts/PlayerController.cs:51)
Regular ray cast are not good for ground detection and stuff. 1. Walk to the very edge of a block or something you will not be grounded and there is one more problem I just forgot. I would you a sphere cast and box cast it’s will cover more thank just a single line so ground checks are more accurate imo
I want to see as a following to this useful video how you cope with the issue of the slope when the player slides down when staying at the slope and when the player is jumping at the slope when going up and down. This is a very common problem I think, but it`s hard to find good information about it.
BTW GUYS, do not use a raycast, use a boxcast, it's similar to a raycast but it simulates a box so if you are still touching the ground but off the edge you will still be able to jump!!!!!
Just a quick doubt, why not use an Empty GameObject with a box collider2D at the bottom of the character to check if the character isGrounded? Raycasting is Wayyy Complicated.
My code and scene is identical but my character very briefly pauses when they hit the ground after a jump. Any idea why? Would love to fix this. Fantastic tutorial though really!
@@SageNode Many of us in the comments have been using a physics material 2d to kind of fix this. I don't think it is perfect but its the best solution so far. Add the material to the ground plane with friction set to 0.01 and bounciness at 0. Have also read something similar but with the material on the player but I haven't tested it yet.
Not sure what I'm missing but when my character lands it gets "stuck" for a short moment when landing. But your character seems to go at the same speed after landing.
You could try setting the "Collision Detection" of your character's Rigidbody2D (I assume you are also making a 2D game) to Continuous. This stopped that weird "sinking in" thing when you land. Hope this works out for you too
Hey I have a question, so when you hold left or right, it makes you go higher when you jump, why is that? Edit: I figured out why, Am looking for a fix, it's because when you are holding down another button along with jump it acts like it cancels it out?
I had some issues with jumping so I started to look at random jumping tutorials, and basically all I needed was to reset the y velocity to 0 on jump. Thank you for that.
@@n1k000z It's so you always get the same velocity no matter how fast you spam the jump button. It can cause inaccurate physics predictions like the jump being executed twice, making you jump extra high sometimes.
"This next part might be a little overwhelming, so I'll explain it slowly." *proceeds to blast through the entire rest of the video* Regardless, very informative and you do explain what you're doing. Maybe just too much all at once for my brain to handle, LOL. It's a me-problem.
I don’t understand why you would make IEnumerators for your sprite squeezing when you could make separate animations for them and store them in a separate layer in your animator
Also if you don’t want to be limited to certain layers to walk on just don’t use a layermask or have a layermask helper script that gets all the lays except what you specify
Hey, Excellent video, if you still answering, i normally use circle or sphere overlapping for detecting ground, I have seen people, like your case that use raycasting, is there any difference? like performance difference? or it doesn't matter in this case?
Hey, I'm probably waaaay late for this, but you're getting stuck on tile colliders. The best solution for me was: 1. Create a new Physics Material with Friction set to zero 2. Attach to the Edge Collider 2D If this STILL isn't working, then: 3. Go to your RigidBody 2D settings 4. Change Collision Detection to Continuous As a note, other people have suggested adding a Composite Collider to your tilemap / ground layer, but that solution didn't work for me for some reason haha Hopefully all this info helps you, or some stranger in the future :)
one thing i notice that using this code, if you have a running start you'll jump much higher than from a standing start because the force is stacked. This could be a cool feature if that's what you intended
@@Charles-Darwin Pretty much this. Halt velocity and have a bool for IsWallJumping. limits inputs to what you want while latched on and set a new animation frame to show collision with wall surface. add impulse at an angle something like 60deg away from wall when jump is pressed and you're done. fixed wall hold vs sliding down would be design choices but the slide down wall can be achieved with a low gravity multiplier. Finish up by wrapping the input in a countdown that drops the character if no 'jump' input is made in time, resulting in the character letting go of the wall. Hope this helps anyone who comes across it.
Hi, Great tutorial and a great channel! I implemented the jump and it works great, i want to implement a Platform Effector to some of my platform tiles but theres a problem since the raycast is touching the platform from below when jumping and it thinks the player touched the ground so it would go all up the platform in some heights, tried shortening the raycast but it doesn't work , any suggestions will be very helpful :)
@@joshuabadgett7987 didn't find a solution for it...probably gonna skip on effectors or if i'm brave enough ill change the raycast method to something else, either a collider box at the feet or the first method in this tutorial : ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-CLxXkSIaOAc.html
Yehiel Certner if you can't change the length it's because (probably) you multiplied (in your script) by ground layer and not ground length . If you didn't make this mistake then just try checking the video again
There is a bug in the code... When standing still, the Linear drag is applied. So when I jump, I cant really jump that high. (The drag changes to 0 when already in the air, but then its too late) However, when running, the drag is 0, and I can jump really high... I dont know how to fix this yet, Ive tried a lot of things...
I got the same problem but I found out the source of the problem. It's the linear drag, that part of the code that's supposed to prevent the player from sliding when it changes directions. Sadly, I don't know the solution but at least the source of the problem was found.
@@memoal-said5111 Toshi 1 second ago Actually I found a better solution that doesnt mess with drag at all, but still gives that smooth movement. void MoveCharacter(float dir) //Dir is the control stick's input. 1 for right, -1 for left and all that. { Vector2 targetVelocity = new Vector2(dir * stats.maxSpeed, rb.velocity.y); //I have my maxSpeed in another script, but whatever. // And then smoothing it out and applying it to the character rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref stats.m_Velocity, stats.m_MovementSmoothing); //SMOOTH DAMP is the key! My Stats movement smoothing is set to 0.05f. Works both when accelerating and stopping. }
Believe it or not. I actually solved this long ago but forgot about this post. My solution was just to use a boolean to check when the player jumps or not and it works. Without that boolean, the issue occurs and I'm surprised that fixed it. XD
9:33 nope... its actually the opposite, holding jumbutton = short jump and tap long jump... looks pretty much like in this video: rb.gravityScale = gravity; if (rb.velocity.y < 0) { rb.gravityScale = gravity * fallMultiplier; } else if (rb.velocity.y > 0 && (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))) { rb.gravityScale = gravity * (fallMultiplier/2); }
I actually had the same issue as you did. My solution was to use a new boolean called "jumped" and set it to false. I added that boolean to the code that handles linear drag to stop the character from sliding. It's this piece of code you should have in your script if you followed the video fully. Void modifyPhysics() { ... If(Mathf.abs(direction.x) < 0.4 || changingDirection) ... Change the if statement to this instead: If ((Mathf.Abs(direction.x) < 0.4f || changingDirection) && !jumped) The jumped boolean turns true whenever the player pressed down the key that makes the character jump and the book turns first if the player touches the ground again. Also, add the bool inside your key press code that has the function Jump() BEFORE it so it works. I hope it works for you like it has for me.
The ray cast method to see if you’re grounded isn’t very good because u won’t be able to jump if your character id more than half way off a ledge u should really use boxcast
When I run on the edge of platforms it "triggers" the squeeze animation, even though I wasn't even jumping, only running back and forth. Am I the only one with this issue? Any thoughts why?
My character loses x velocity when he hits the ground after a jump. This feels like he is dragging a bit after every jump. What can be causing it? Did you change the physics material of your tiles?
Guys, I need help. The part where the player has high jump and low jump isn't working. My code is the same as in the tutorial, but for some reason, when I press "jump" it only goes so high, no matter if I hold or just tap the button.
Add propterties extraJumpTotal(starts at 0) and extraJumpMax(1 for a normal double jump and 2 for triple jump). Then, where you check for jumpTimer && onGround add and else if to see if you have extra jumps left. if(jumpTimer > Time.Time && onGround){ jump(); }else if(extraJumpTotal < extraJumpMax ){ jump(); //
The linear drag is 0 when running, and 4 when standing still. When jumping, the height of the jump is affected by the values I mentioned. I don't know how to fix this problem. When standing still, I cant jump that high. only when running, I get max jumping potential. Anyone else getting this problem and know how to fix it?
Actually I found a better solution that doesnt mess with drag at all, but still gives that smooth movement. void MoveCharacter(float dir) //Dir is the control stick's input. 1 for right, -1 for left and all that. { Vector2 targetVelocity = new Vector2(dir * stats.maxSpeed, rb.velocity.y); //I have my maxSpeed in another script, but whatever. // And then smoothing it out and applying it to the character rb.velocity = Vector3.SmoothDamp(rb.velocity, targetVelocity, ref stats.m_Velocity, stats.m_MovementSmoothing); //SMOOTH DAMP is the key! My Stats movement smoothing is set to 0.05f. Works both when accelerating and stopping. }
Hey! Great tutorials! But one thing, I've tried this tutorial now from scratch acouple of times. My character seem to jump the same distance, no matter how hard or light I press the jump button... Don't know if there's something I'm missing :(
Wow, I haven't even finished the video yet but this is amazing. You explain everything perfectly and lay out the videos perfect for beginners. You deserve so much recognition, keep up the amazing work.
hi there. first of all, thanks for this great tutorial. its very very useful for me. And second, I realised that you used a few second part of brackeys video at 1:30 but at the end of tutorial you didnt thank to him :) just reminding ;) thanks again for tutorial...