2D Character stick on my floor ?

Hi,

My Character is the little square.


I want to mo my character along the collider I selected, (no “real” gravity, just along the collider).

How can I make that ?

Thank you.

Best regards,

AB

In your example, is your character inside your collider? You mean to move it along the grey track on the edge? If that’s the case And you want to track the outside edge of the collider you can probably use raycasting from the bottom of your player to get the closest edge of the collider, and I’m pretty sure you can use the collison info to also angle your character’s rotation to match the plane of the collider’s edge…

Hi,

Thanks for your help.

My character is the little square on the bottom left.

How can I do what you said ? I’m sorry it’s the first time I tried to do this …

I search all day long but I didn’t understand how to do this.

Thanks a lot

Sorry I thought the your character was the object at the center for some reason lol. Well, I’ll try to give you a better idea of what I mean… hopefully it will be a good jumping off point… In my pathfinding routine I use the following code to see if the path is clear for sprites to approach their targets, however, looking up these Classes and the method headers in the API should get you on the right track. The code below creates a var hit to store a RaycastHit2D object that is returned from Physics.Raycast(). You can see what kind of info it makes available in the API.

// Cast a ray toward the target.
    var hit: RaycastHit2D;
    hit = Physics2D.Raycast(rigidbody2D.position, direction,
        rayDistance, 1 << LayerMask.NameToLayer("Background"),
        0, Mathf.Infinity);

Perhaps you can use this cast a few rays to determine the geometry below the cube, say one straight down, and at least one more to make sure the bottom of the cube is parallel to the surface(? You can have a set distance you want the cube to float, and perhaps adjust the rotation and velocity of your cube if that distance changes, ie the floor drops out or comes up at the cube. Two other rays say, plus or minus 30-45 degrees from center down, or maybe just one can track upcoming changes in their distances that might aid your calculations (I guess, or make them more complicated lol). Hope this was of some use. Also, in searching, tho I haven’t checked it out fully, I found a guy who was trying to simulate faux gravity like in Mario Galaxy. Might be of some help too, so here is that link.