Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Walk around 2d-platform by manipulating gravity direction

Discussion in '2D' started by rboerdijk, Nov 5, 2021.

  1. rboerdijk

    rboerdijk

    Joined:
    Aug 4, 2018
    Posts:
    96
    Assume we have a capsule representing a character and each character has its own "gravity-direction" (black arrow) and a "right" direction perpendicular to the gravity direction (red arrow).

    We can define a position slightly "ahead" (green circle) to scan where the ground ends. Doing this it's easy to make the character flip and walk in the other direction.

    It gets a bit more interesting when arriving at the corner of the "groundbox" to start manipulating the gravity direction to smoothly walk around the corner...
    walk-around-platform.png
    Trying to think this through, one idea would be to smoothly change gravity somehow around the curve. Could do some raycasts downward (or maybe downward in a slight angle, but I don't see how to get a nice gravity change this way).

    Or alternatively "flip gravity in 90 degree angles" instantly and then rotate the character sprite over a few frames to "catch up" with the new gravity direction - but the moment of doing that is critical, avoid popping but also avoid the character from falling down because we flip to early and him then falling "down" on the left side of the screen (falling over the floor we just walked over).

    Any ideas anyone how best to approach this ?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,512
    You wouldn't change global gravity unless this was the only thing affected by it surely?

    Gravity is the same as you applying an impulse (instant "force") so add your own gravity. For a capsule, as you contact that box (as per your diagram) the collision normal of the single contact on the surface will point up then as you contact the corner vertex it'll rotate so that eventually it'll point right. As long as you stay in contact (or you get this normal from performing a CapsuleCast) then you'll have this direction. Opposite this direction can be the direction to apply your impulse.

    You will have to ensure that the rotation is constrained too and rotate it yourself using MoveRotation. The rotation is the angle of the collision normal.

    This is a "local gravity" but really it's just a way to make it stick to surfaces so that "up" is always perpendicular to the surface.

    Note: I see so many devs using raycasts as if they are the best way to do this. A raycast does NOT represent your capsule shape so you're already starting with a bad approximation. Use the actual collider shape to cast. There's a whole bunch of queries for this.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    As usual, I agree 100% with @MelvMay 's notes above... let me also add my experience:

    This is a difficult one to get right in the general sense. I know because I've tried making a little 2D spider guy that crawled around a cave. It's doable, but fiddly, and it gets harder the tighter the other nearby colliders are.

    The hardest way is to analyze the level on the fly (with various types of casts) and try to creep around corners in all cases, such as with tight crevices and pointy parts.

    A more labor intensive way is to hand-edit the level and put in trigger colliders that define the correct gravity at each point in space. If you take that approach you can get it working perfectly in all cases, at the cost of having to hand-draw those triggers for every level every time.

    You might also try to write tooling to pre-process the level in advance so you have a chance to detect bad geometry (generally anything too high-frequency) that causes issues.
     
    MelvMay likes this.
  4. rboerdijk

    rboerdijk

    Joined:
    Aug 4, 2018
    Posts:
    96
    Thank you both for your insights!

    I should have clarified this is using kinematic, and my thought was to have each character have a (different) gravity direction pointing towards the floor (rather than manipulate global gravity).

    I was stuck in the idea of using a raycast or a hardcoded offset to 'scan the floor' (which works perfect for character which turn around near the edge), but indeed a capsule/sphere downward will result in a nicely rotating hit normal which can be used to determine the gravity-direction when hitting those corners - that solves the exact thing I was struggling with.

    The other thing I realised is that while I do need the gravity-direction - that is only really needed to know what the upAxis (=negatived gravity) and more important the right-axis are. So when that gravity changes direction, so does the right-Axis which is what we use to keep the character walking around the platform.
    You don't really need to apply a gravity force to keep the character "stuck to the wall" to compensate for "real gravity" because there is no real "downward" gravity because of using kinematic instead of actual physics. In hindsight it's actually really obvious, but intuition was screaming the character would need to be pushed towards the wall, at least in case of the sides and bottom, to not fall down the screen.

    Will experiment more with this, thanks again!
     
    Last edited: Nov 7, 2021
  5. SilexPixels

    SilexPixels

    Joined:
    Nov 11, 2021
    Posts:
    2
    Hello :) Would you mind explaining your process step by step by any chance?

    I'm pretty new to Unity and I've been searching the internet for ages for a way to create this "2D spiderguy crawling on all angles" mechanic, with very little success. I'm trying to do exactly what you did.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Here is an approach you can use to figure out what is happening in the functioning code / package above:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.
     
  7. SilexPixels

    SilexPixels

    Joined:
    Nov 11, 2021
    Posts:
    2
    Thank you very much for you answer Kurt, my only issue is that I couldn't find any single tutorial on how to have a 2D character walk on all sides of a plateform like shown in the picture above.

    I would love to copy any tutorial and study every bit of it but there's just none on the matter, by either Brackeys or anybody else. It seems like the way people do it is by creating there own personal code each time. That's why I asked you specifically how you did it.

    I'm not an english native speaker, I apologize if I'm not clear enough.

    Have a good one!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Steps to success:

    - do tutorials... ANY tutorials, and do them properly (See 2 steps above)

    - tutorials, done properly, will expose you to concepts used in Unity: how things connect, how things relate, how to use Unity

    - this knowedge will enable to study the project I have provided above

    - every time you reach a part you don't understand, start doing more tutorials on how to use Unity, how to understand what is in your project or in the tutorial or anything

    Eventually you will become adept at understanding how a project works.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Also, this approach might appeal to your learning style:

    Imphenzia: How Did I Learn To Make Games: