3 ways to do a Ground Check in Unity

Sdílet
Vložit
  • čas přidán 10. 09. 2019
  • ✅ Get the Project files and Utilities at unitycodemonkey.com/video.php...
    Let's look at 3 different methods for doing a Ground Check which is a necessity if you're making a Platformer.
    If you have any questions post them in the comments and I'll do my best to answer them.
    🔔 Subscribe for more Unity Tutorials / @codemonkeyunity
    See you next time!
    📦 Grab the game bundle at unitycodemonkey.com/gameBundl...
    📝 Get the Code Monkey Utilities at unitycodemonkey.com/utils.php
    #unitytutorial #unity3d #unity2d
    --------------------------------------------------------------------
    Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.
    I've been developing games for several years with 7 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.
    You can see my games at www.endlessloopstudios.com
    --------------------------------------------------------------------
    - Website: unitycodemonkey.com/
    - Twitter: / unitycodemonkey
    - Facebook: / unitycodemonkey

Komentáře • 431

  • @rupeshpandey6603
    @rupeshpandey6603 Před 3 lety +25

    For anyone who is wondering, What was the Drawray thingy, Here it is.
    Debug.DrawRay(boxCollider2d.bounds.center + new Vector3(boxCollider2d.bounds.extents.x, 0), Vector2.down * (boxCollider2d.bounds.extents.y + extraHeightText), rayColor);
    Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, 0), Vector2.down * (boxCollider2d.bounds.extents.y + extraHeightText), rayColor);
    Debug.DrawRay(boxCollider2d.bounds.center - new Vector3(boxCollider2d.bounds.extents.x, boxCollider2d.bounds.extents.y + extraHeightText), Vector2.right * (boxCollider2d.bounds.extents.x * 2f), rayColor);
    Debug.Log(raycastHit.collider);

  • @paleSquash
    @paleSquash Před rokem +10

    For anyone struggling at the 5:50 mark on getting the debug raycast to show up: Make sure IsGrounded() is called before checking for the jump key. C# implements something called "shortcutting" which means that if you have 2 AND conditions in an if statement and it fails the first one, it won't even check for the second one. If the IsGrounded() is in the second one, then it won't draw the raycast. Just something to help :)

  • @ignacioarenasguerra9696
    @ignacioarenasguerra9696 Před 3 lety +114

    It's necesary to make a change in the BoxCast. You need to aply a size correction for avoiding the situation when the character is next to a wall and the isGrounded function detects the Wall and let you jump again and again.
    Code:
    RaycastHit2D raycastHit = Physics2D.BoxCast(body.bounds.center, body.bounds.size - new Vector3(0.1f, 0f, 0f), 0f, Vector2.down, extraHeightTest, platformLayerMask);
    PD: Sorry for my english :)
    PD2: This video was awesome!

    • @bob7388
      @bob7388 Před 3 lety +3

      Thank you :D

    • @SMT-ks8yp
      @SMT-ks8yp Před 3 lety +8

      Or you can exploit this for walljumps, I guess.

    • @raulsangonzalo
      @raulsangonzalo Před 3 lety +1

      you also have to add 0.1f to the y to prevent grounded when you hit the roof!

    • @GSBroker
      @GSBroker Před 3 lety +8

      @@SMT-ks8yp You joke, but this is how many creative features in games are born -- they start as bugs.

    • @user-xg4np9gu4d
      @user-xg4np9gu4d Před 2 lety

      you can also reduce the size of the collider a bit.

  • @NuNaKri
    @NuNaKri Před 4 lety +18

    Perfect! Now I can decide which method to choose and why :D Thanks a lot :D

  • @Baal3033
    @Baal3033 Před 4 lety +20

    Awesome tutorial. I mainly watched it to get a better grasp of Raycasts and checking for colliders and now I also know about LayerMasks, BoxCasts and Collider.bounds. Thanks :)

  • @thebrunorobert
    @thebrunorobert Před 4 lety +2

    Simple and concise, thank you!

  • @markedforstrike
    @markedforstrike Před 4 lety +1

    Dat thing is one that always needed and always forgotten by me. Thanks!:)

  • @zacharyferioli1214
    @zacharyferioli1214 Před 2 lety +1

    Thank you! I have been stuck on this. I have been trying to use a box collider as a trigger and tell the script to check for ground that way, which didn't seem to work how I wanted it to for the game that I am making. I clicked on your video and got it done in a matter of five minutes. Also, thank you for explaining what everything did instead of just telling us to copy down some code and just trust that it works. Definitely subscribing

  • @humadi2001
    @humadi2001 Před 3 lety +1

    always find you when I'm looking for The Best Code, Thank You🌹.

  • @dhankumari7290
    @dhankumari7290 Před 4 lety +38

    Your teaching style is utterly different than other people that i have seen.
    you are different and fully confident in your craft. hats off your confident and experience that i learn so much important things
    May you and your family live long !!!

  • @unitynocode
    @unitynocode Před rokem

    You are the best. I stuck in this issue many months ^ ^ now it's done. Thank you so much !

  • @diliupg
    @diliupg Před 4 lety +1

    Concise. Great explanation. Good tutorial. Thank You.

  • @jiper94
    @jiper94 Před 3 lety

    Thanks man, you're always such a huge help

  • @marvelmovieshd4436
    @marvelmovieshd4436 Před 2 lety +1

    Thank you soooo much man, your tutorials are very helpful

  • @cristiano4826
    @cristiano4826 Před 4 lety +4

    Thanks for the explanation 👍

  • @vihaannagarkar1870
    @vihaannagarkar1870 Před 3 lety

    Loved this video, it helped me a lot. Thank you very much!

  • @akshaymanthekar1032
    @akshaymanthekar1032 Před rokem

    Hey Hugo, you are a huge support for the beginner game devs like me. Keep up the good work man! I really appreciate it :)

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před rokem +1

      Thanks for the kind words! I'm glad you found the videos helpful!

  • @Ivanho
    @Ivanho Před 4 lety

    you saved my life, thank you very much

  • @jahoopyjaheepu497
    @jahoopyjaheepu497 Před 4 lety

    Good video explaining the basics. Building out a full controller using raycasts/boxcasts is a lot of work.

  • @hichamlaqrafi2885
    @hichamlaqrafi2885 Před 4 lety +1

    Wow ! somehing great today
    Good explanation Perfect work

  • @danielsantana5126
    @danielsantana5126 Před 4 lety +3

    I loved your channel because It is easy to understand your english :)

  • @bazdonaxdumaul1493
    @bazdonaxdumaul1493 Před rokem

    Solved my problem, TYVM.

  • @heroofthyme4237
    @heroofthyme4237 Před 4 lety +16

    You are a legend and deserve so much more love for sharing all your knowledge

  • @sirmicah
    @sirmicah Před 4 lety +7

    Hey thanks for this man its really helpful. I'm taking an online Unity course and the instructor had us use raycasting and that didn't work at all for the game and the recast was off center and it was just a mess. I needed a different way of doing it.

  • @Maukustus
    @Maukustus Před 3 lety +6

    thank you monke.

  • @drunkengaming4782
    @drunkengaming4782 Před 2 lety

    Saved my project thank you so much

  • @jesudunniakinyemi2979
    @jesudunniakinyemi2979 Před 2 lety

    Thanks a lot
    this was really helpful

  • @Real_MrDk
    @Real_MrDk Před 4 lety +1

    Lots of love from India BRO keep it up

  • @djdatore
    @djdatore Před 3 lety +2

    This helped a lot. What do you do in a case where you bump into a floating platform and touches platforms from the side?

  • @gamgnamstile
    @gamgnamstile Před rokem

    Thanks, very helpful!

  • @hermannlagrange803
    @hermannlagrange803 Před 3 lety +11

    4:17 Just some free advice, if you're going to show how to change the colour of the ray cast, don't just make the variable and assume the viewer knows what to do with it. Show the whole code, don't cut parts of it off.
    The code is supposed to be: Debug.DrawRay(boxCollider2d.bounds.center, Vector2.down * (boxCollider2d.bounds.extents.y + extraHeight), rayColor);
    but you never show the part where you add the 'rayColor' to the line, you assume we know to do that.

  • @stevancosovic4706
    @stevancosovic4706 Před 2 lety

    Oh, thank you man!!!

  • @TinyCastleGames
    @TinyCastleGames Před 2 lety

    Great tutorial, Thanks

  • @djeka415
    @djeka415 Před 2 lety

    Thank you!

  • @skippynik6003
    @skippynik6003 Před 3 lety

    thanks man you are awesome!!!!

  • @ritvik4835
    @ritvik4835 Před 3 lety +2

    saved me thanks !!!

  • @ThienNguyen-mo9xv
    @ThienNguyen-mo9xv Před 4 lety

    Cool instruction!!!

  • @at930pmgames
    @at930pmgames Před 3 lety

    really helpful thanks

  • @yalieyal4362
    @yalieyal4362 Před rokem

    Thanks!

  • @mohamedessam-6228
    @mohamedessam-6228 Před 4 lety +2

    love you :>

  • @manuelfont4494
    @manuelfont4494 Před 4 lety +4

    Some help:
    Note that if you check if you are pressing the space bar before checking if you are in the ground the ray will only be visible when you jump.

  • @saywhat1688
    @saywhat1688 Před 3 lety

    Thanks a lot . This is the only method that worked for me. I had tried 4 different methods before and none of them worked.

  • @danmolo
    @danmolo Před 2 lety

    Thanks a lot!!

  • @masteratt
    @masteratt Před 4 lety +12

    Nice tips, thank you.
    Just curious if there's a reason you haven't updated your Unity to 2019?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 4 lety +10

      Heh funny about that. This particular video was actually recorded all the way back in April but somehow I completely forgot to upload and last week when I was preparing to get back to regular videos I found it. There's still one more video from that time that I found as well.
      In the Radar Chart video which was recorded and published on Sunday you can see I'm using Unity 2019.

  • @duyvo1258
    @duyvo1258 Před rokem +1

    Another method is to have another game object like the third, but instead of having entire new script, we can use a reference to that object in the main script and write a function which will draw a OverlapCircleAll and it's actually pretty similar to BoxCast!!

  • @spedotgame4916
    @spedotgame4916 Před 4 lety +1

    Yeah, Thanks so much

  • @ordepsmusic
    @ordepsmusic Před 4 lety

    Perfect class

  • @androxz7
    @androxz7 Před 4 lety +18

    Nice video, mostly I work on mobile so I consider the last method to be really expensive since you are calling a getcomponent in update and also I notice that OnTriggerStay is also expensive on mobiles why I highly avoid it, so I try to do those kind of booleans in OnTriggerEnter and make them false in OnTriggerExit, so I recomend using the first and second if you are working on mobile, also call it every 3 or 5 frames to increase performance and not every update.

    • @1lsgaming27
      @1lsgaming27 Před 4 lety +2

      Nice tip

    • @fiQmeister
      @fiQmeister Před 3 lety +1

      How do you call it every 3-5 frames?

    • @androxz7
      @androxz7 Před 3 lety +4

      @@fiQmeister usually an if(Time.framecount % "desired amount of frames" == 0)

  • @dinozaurwrs
    @dinozaurwrs Před 3 lety +1

    thanks for unlocking my mind bro

  • @BmoreGaming
    @BmoreGaming Před 7 měsíci

    I know this might be a little unrelated to the video, but how do you check the ground material to play different sounds. For example, if a player is running on grass and then transitions to a stone road, what is the best way to check for this to change the audio when they are on different surfaces? I've read some people use raycasts, but that sounds like it could be expensive especially if there are 100s or even 1000s of players all using raycasts to check for audio. Any suggestions?

  • @xxyxxyyyx
    @xxyxxyyyx Před 3 lety

    One question to the box cast, can I detect on what site the boxray collided with something or just that it collided at all?

  • @denis.klyuev
    @denis.klyuev Před 4 lety

    Supercool!

  • @CC_-pn2og
    @CC_-pn2og Před 3 lety

    @CodeMonkey why does it trail behind the player instead of being on top if the player? sence the box colider is not trailing behind the player and is on top of it... id understand why it is not, and i dont know how i would add the velocity of the player on to it causes it to not be able to jump if i am moving towards a ledge because it is behind me when moving. i might of just missed this part in the tutorial or messed up somewhere

  • @Rovsau
    @Rovsau Před 3 lety

    Talk about superior method - I just found the superior channel ♥

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 3 lety +1

      I'm glad you like what you see!

    • @Rovsau
      @Rovsau Před 3 lety

      @@CodeMonkeyUnity This video was great for grasping the concept and understanding the code. I watched it several times. Both at 0.25 and 1.25 playback speed xD
      After some tinkering I figured out a way to make a single Raycast on the bottom side of the character work as a ground check (horizontal). Raycasts are infinitely thin and normally can't register the collider from which they originated or started inside, but for some reason it works with Physics2D, and I'm using the same approach for Wall Check Left/Right.
      I will probably end up with a Boxcast due to issues with slopes, unless I introduce another ray, but I'm testing it for now since I was able to write it myself.
      Do you have any opinions on Ray/Boxcast vs programming Collision methods?
      Edit: Just realized my Raycasts only worked because I wasn't using a composite collider on the Tilemap. They were always long enough to find another collider.

  • @M1lkshaking
    @M1lkshaking Před rokem +1

    For those struggling to implement the first method in 3d at 2:08: if you are using a box collider like me, you can put (GetComponent().center) to get the center

    • @galerez2250
      @galerez2250 Před rokem +3

      You shouldn't call GetComponent() in Update/FixedUpdate etc.. you should cash the collider, use BoxCollider boxCol = getComponent() at awake or use SerializeField

  • @Mimi_py
    @Mimi_py Před 3 lety

    thank you

  • @ginodekovic2733
    @ginodekovic2733 Před 4 lety +1

    Is there a way to make this with capsule cast or something similar for 3D

  • @siddharth7441
    @siddharth7441 Před 4 lety +1

    great video

  • @neozoid7009
    @neozoid7009 Před rokem

    Hey CM awesome video, But we can also use the player collider for the ground check then why to use a raycast ?

  • @miaoumixed4268
    @miaoumixed4268 Před 2 lety +8

    Hi, thanks for this video.
    I used to create two empty GameObject(s) under the "Player" and an "OverlapArea". It works very fine as it creates an easy setting rectangle box where I want.
    But, I like your method as there is no added object to the "Player" and only use a simple calculation called when needed.
    Thank much.

  • @user-dy5hj7vq9l
    @user-dy5hj7vq9l Před 4 lety +1

    What do you think about use to Physics2D.OverlapCircle()?

  • @yerasisaiprasadreddy
    @yerasisaiprasadreddy Před 4 lety +2

    Even in OnTriggerExit2D we should check if the collider that is exited is the ground collider

  • @Jb-vv1ur
    @Jb-vv1ur Před rokem +1

    good video but why is the boxcast superior to the second hitbox when they both function the same exact way?

  • @eriknastesjo843
    @eriknastesjo843 Před rokem

    Hi! I went for nr 2 (BoxCast), checking for ground in Update and moving player in FixedUpdate. I noticed that makes the BoxCast (checking for ground) ahead of the players collider when he moves. If I put both checking for ground and moving player in FixedUpdate the reverse happens.
    EDIT: Turns out it was my rigidbody2d that was on "interpolate: interpolate" which made is "lag behind" the more choppy moving collider. So it seems to me that you will be of forced to have no interpolation if you want to have ground detection aligned with collider at all times. Seems to me that nr 3 might be a better option after all...

  • @ChuckSploder
    @ChuckSploder Před 2 lety +1

    In the newer versions of unity you can just set the layer of your player as 'Ignore Raycast' as the fix for that bug.

  • @babymusou9208
    @babymusou9208 Před 3 lety +1

    damn he really called me out on the last one 😂

  • @jamado9067
    @jamado9067 Před 3 lety +8

    9:36 Recommended Method

    • @allwoundup3574
      @allwoundup3574 Před 3 lety

      by whom?

    • @jamado9067
      @jamado9067 Před 3 lety

      @@allwoundup3574 what do you mean?

    • @allwoundup3574
      @allwoundup3574 Před 3 lety

      @@jamado9067 By whom is that method recommended?

    • @jamado9067
      @jamado9067 Před 3 lety +2

      @@allwoundup3574 code monkey says it is the superior method (12:05)

  • @wizardstudio3633
    @wizardstudio3633 Před 4 lety

    tnx

  • @randomaccount594
    @randomaccount594 Před 2 měsíci

    instead of setting all ground objects to a layer you can just set your player to the "Ignore Raycast" layer. also, for extra height, 0.01 may not work for everyone depending on your hitbox and/or collision detection method

  • @eduardsdavis903
    @eduardsdavis903 Před 3 lety

    Can you show me how to make a 3D movement that can climb stairs or move on sloping surfaces using a rigidbody?
    Is it possible to make a player turn on the slope of the ground?
    I like to use a charactercontroller, but I can't because the charactercontroller is round, but my player is cubic and if I approach a wall, then the wall enters the player, until it touches the charactercontroller's shell.
    I'm a beginner and I've been trying to do it for 2 weeks now, but there is no way it works. I hope you can help me!
    Thanks!

  • @sebastiangajardo9622
    @sebastiangajardo9622 Před 4 lety

    i believe there is easier ways to do the 3rd one but maybe the way i use is not the best, still good video, i learn more than what i was looking for.

  • @ashkankiafard3080
    @ashkankiafard3080 Před 4 lety +5

    Hey,thanks for the tutorial, but I have a problem with the third method :(
    My isgrounded_check doesn't get false after jumping.I'm pretty sure I did everything right and I can't found the problem.
    I also tried the 2 first methods and the problem was my character was unable to jump.
    I would appreciate it if someone helped me out!
    p.s:The size of my box colliders are right

  • @Astorec
    @Astorec Před 4 lety

    Hey!
    Followed along for a bit, I current;y have the IsGrounded() method, but my issue that my Colour doesn't appear at all when it's started and only appears when Space is pressed.
    Also I can still jump. I am using a Capsule Collider though instead of a Box collider. Is this maybe the cause of it?
    CpasuleCollider2D cc;
    void Start()
    {
    cc = GetComponent();
    }
    bool IsGrounded()
    {
    float extraHeightText = 0.01f;
    RaycastHit2D hit = Physics2D.Raycast(cc.bounds.center, Vector2.down, cc.bounds.extents.y + extraHeightText,groundMask);
    Color rayColor;
    if(hit.collider != null)
    {
    rayColor = Color.green;
    }
    else
    {
    rayColor = Color.red;
    }
    Debug.DrawRay(cc.bounds.center, Vector2.down * (cc.bounds.extents.y + extraHeightText));
    Debug.Log(hit.collider);
    return hit.collider != null;
    }

    • @yalieyal4362
      @yalieyal4362 Před rokem

      even though i am extremely late:
      The DrawRay only appears if you run the function IsGrounded, which in this scenario is only when you hit spacebar.
      If you check again in the video you will see there is a PlayAnimations(IsSrounded()); in update that draws the ray (and any other animations that would be in the function) whenever the PlayAnimations() is called (in this scenario every update)

  • @PumaFau
    @PumaFau Před 4 lety +1

    How do you do this when your ground is just an edge collider on a tilemap?

  • @365daysofhowto7
    @365daysofhowto7 Před 4 lety

    3rd one is awesome
    But understanding raycast may help in developing shooter games

  • @luizgustavodesouza9334

    Not super important, but I'm curious. Why did you put move key mappings in FixedUpdate() and the jump key in Update()? Is there a big difference between the two functions? I heard elsewhere that FixedUpdate() is faster, so I'm doing everything there as I learn things

    • @grizzledwarveteran2321
      @grizzledwarveteran2321 Před 3 lety

      Fixed update is better for physics like moving left and right, but things like jump, where it’s a get key down, you want it to be able to respond reliably and quickly. If you try jump on fixed you see it’s inconsistent

  • @uyscuti5571
    @uyscuti5571 Před 4 lety +1

    nice video!!!! but is it ok to use tages insted of layermasks?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 4 lety +2

      You can but don't use tags. They are string based which makes for some very nasty code since everything can easily break by just mistyping a single character.

    • @uyscuti5571
      @uyscuti5571 Před 4 lety +1

      @@CodeMonkeyUnity i agree thank u so mush

  • @youtoobar7139
    @youtoobar7139 Před 3 lety

    it jumps if the mask layer = everything but if I specify a layer put it on my colliders and select that layer it just doesn't move
    if I change the character's layer mask it does start to jump but returns the same problem at the start

  • @INFPownage
    @INFPownage Před 3 lety +1

    how do I make sure my jump animation is active when the groundcheck is not interacting with the platform layer? I am going with the 3rd option.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 3 lety +1

      Add a boolean parameter to your Animator and set it to the same as your isGrounded bool.

  • @archieaullikan6757
    @archieaullikan6757 Před 2 lety

    Taught well but could've summarized previous videos as I got stuck with trying to figure out why the code wasnt working only to realise I had missed your get objects in the first few lines of code which cost me a lot of time and energy

  • @xnaturell9189
    @xnaturell9189 Před 4 lety +6

    It doesnt let me write bounds after boxCollider2d (1st method) why?
    it says that object doesn't contain a definition for bounds

    • @dudeguyman2618
      @dudeguyman2618 Před 3 lety

      maybe your getting the component incorrectly, or you're getting the wrong component

    • @o0l1e3q9
      @o0l1e3q9 Před 3 lety

      you should declare it and GetComponent it in the Awake function

  • @josephelliott2325
    @josephelliott2325 Před 4 lety

    I haven’t tried it yet but if you were directly against a wall with they box cast wouldn’t you be able to jump more than once since the box is touching the wall the entire time?

  • @vutranminh1886
    @vutranminh1886 Před rokem

    I had used isGround in Update and Physics2D.BoxCast is jerked when i use transform.position for movement of gameobject. Help me, pls !!!

  • @rafaelochoamello9867
    @rafaelochoamello9867 Před 4 lety

    i'm new to coding in Unity/C# and i'm using the 'classic' "OnCollisionEnter2D()", and using the CompareTag to check the ground, this is a viable way of do this, or its better using your box method ? Sorry if this is a dumb question, but i like your videos and i really wanna know the best way of do this.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 4 lety +1

      Tags are very brittle due to them being string based so I'd avoid them at all costs.
      If you are just doing a ground check then I'd recommend the BoxCast method.

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety

      there is no best way, it depends on the situation.

  • @Luca-nq4gy
    @Luca-nq4gy Před 3 lety

    Is this ok if i using collider in 3d?
    because i need OnTriggerEnter and OnTriggerExit for trigger animating landing and jumping or falling before jumping.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 3 lety

      Yes, instead of using 2D components and OnTriggerEnter2D you use 3D components and OnTriggerEnter

  • @dynamicvoltage9765
    @dynamicvoltage9765 Před 3 lety

    The third option is dangerous because it will allow you to jump when you're pressed against a wall, even if you're not grounded. You have to make the ground box smaller along the X axis, but when you do that, it causes other issues.

  • @superdahoho
    @superdahoho Před rokem

    wait, so why is the boxcast method superior to the last (beginner) method you described?
    it passes both the slope and edge case.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před rokem

      It does not require an extra Game Object with an extra collider. Personally I find it much better to handle all that logic through code, and if you have many NPCs/Enemies then that extra game object on all of them can be a source of performance issues.

  • @midiman22000
    @midiman22000 Před 2 lety

    So, I know this video is 2 years old, but I have a question. The boxcast method works great for regular platforms, but if I have semi-solid platforms (I can jump through from the bottom) the Boxcast recognizes being inside the semi-solid as a platform and I can jump again. How would I go about changing the size of the boxcast to, basically the character's feet only?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 2 lety

      You do a BoxCast to find the object then compare the player position and the platform position. If there's a collision but the player is under the platform, don't consider that grounded.

  • @elderGS00
    @elderGS00 Před 3 lety

    i used the third method and changed this line
    isGrounded = other != null && (((1

  • @domo99ify
    @domo99ify Před 4 lety

    Nice, but between box collider and boxCast, which is more optimal?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 4 lety

      The BoxCast is the superior method since it just requires one line of code whereas the BoxCollider requires another game object and another script.
      The benefit of the BoxCollider is that it's easier to visualize.

  • @nanr1686
    @nanr1686 Před 3 lety

    when i put in the grounding code unity says this "Cannot assign to 'isGrounded' because it is a 'method group'" plz help

  • @NST-games
    @NST-games Před 4 lety +1

    But what if you also have an effector platforms? My character becomes grounded on a moment he jumps up through such a platform from a bottom.

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety +1

      check your player's y velocity, if you are moving up you can disable the collider or not do the collision and when you are still or moving down you enable it/do the check.

  • @MrRobotrax
    @MrRobotrax Před 4 lety

    Is there any way to get the ground normal from a 3d boxcast? I need that player to not be able to walk on surfaces with too much slope.

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety +1

      You could use two rays one on each side then you can get the slope from the distance of the 2 collisions.

  • @xuraith
    @xuraith Před 4 lety

    what would you do if you turn on Composite Collider 2D and the walls and celing become ground layer? I like having 1 continuous collider with no gaps, but, it causes infinite wall jumps or no jumping at all if you change it from ground layer.

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety +1

      well if you use the ray method it should never touch your walls or ceiling unless your character does flips.

  • @SociopathDev
    @SociopathDev Před 4 lety +2

    What about when you have a platform in above you and you jump over it? I'm struggling with that part since it says i'm grounded before my character is fully stopped on the platform. I mean using platform effector

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  Před 4 lety +2

      Do you mean your character is ground when it's touching the sides of the platform? If so you can probably solve it by making the BoxCast a tiny bit smaller on the width.

    • @SociopathDev
      @SociopathDev Před 4 lety +1

      @@CodeMonkeyUnity I mean imagine there is a thick plataform on top of you and it has an platform effector 2d, so when you are jumping and your box collides with the platform/layer it will say you are grounded in the middle of the process to get on top when you are still jumping

    • @AsciiKing
      @AsciiKing Před 4 lety +1

      @@SociopathDev Are you saying you want to be able to jump through a platform above you and then be able to land on it?

  • @lucasteles42
    @lucasteles42 Před 4 lety +6

    O dont get why BoxCast is the best option over a collider. What is the advances and disadvantages of those alternatives?
    Thanks, great video 😁

    • @ShenDoodles
      @ShenDoodles Před 4 lety

      The boxcast checks the entire ground underneath the player. As demonstrated in the video, only this will allow jumping on slopes.

    • @Semaj0z
      @Semaj0z Před 4 lety +1

      @@ShenDoodles so does the box collider

    • @AsciiKing
      @AsciiKing Před 4 lety +4

      @@Semaj0z I didn't hear him mention it in the video, but I think the reason the box collider is inferior is because it will check for collisions around the entire box. So, if a player jumps up and bumps their head on the platform above them, they would then be allowed to jump up again because the collider would be touching a platform.

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety

      @@ShenDoodles it's not the only way :P

  • @tayyabimtiaz8774
    @tayyabimtiaz8774 Před 4 lety

    Can you please tell me where to invoke animation. I call the animator and set the bool to play the animation when not grounded. But there is some weird behavior. The function is being repeatedly called in the update. Please guide me with the best practice to play jump animation....

  • @matheusreidopedaco
    @matheusreidopedaco Před 3 lety +1

    For some reason my RayCast is pointing to another direction?

  • @loveforallbxlmannif
    @loveforallbxlmannif Před 3 lety

    I have one another method but i'm not sure it's good and plan to maybe change it.
    I change the velocity of the rigidbody that have no mass manually. Just before that in the code i check if the velocity value i send and the actual velocity of the rigidbody are different. If the player or object stop falling it's a ground check.
    For now it work. But i want to make moving platform that will maybe break it.

  • @ampeyro
    @ampeyro Před 4 lety +6

    But...
    Isn't there a way to get it straight from the box collider?
    After all, some part of the program has to already know it for the character to not fall through the ground.

    • @AIAdev
      @AIAdev Před 3 lety

      You could put a script on your player that uses OnCollisionEnter() and OnCollisionExit() methods to set the isGrounded bool. Then within those methods check for a specific tag like "ground. For example within OnCollisionEnter you could do a... if(other.CompareTag("Ground")){ isGrounded = true;}

    • @matthewtimmermans2610
      @matthewtimmermans2610 Před 3 lety

      there is collider.IsTouching(otherCollider);