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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Character stick on my floor ?

Discussion in '2D' started by ababab5, Jan 4, 2015.

  1. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    Hi,

    My Character is the little square.
    Capture d’écran 2015-01-04 à 20.28.42.png
    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
     
  2. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    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...
     
  3. ababab5

    ababab5

    Joined:
    Apr 15, 2014
    Posts:
    508
    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
     
  4. BindingForceDev

    BindingForceDev

    Joined:
    Aug 13, 2014
    Posts:
    40
    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.
    http://docs.unity3d.com/ScriptReference/RaycastHit2D.html

    Code (JavaScript):
    1. // Cast a ray toward the target.
    2.     var hit: RaycastHit2D;
    3.     hit = Physics2D.Raycast(rigidbody2D.position, direction,
    4.         rayDistance, 1 << LayerMask.NameToLayer("Background"),
    5.         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.
    http://forum.unity3d.com/threads/faux-gravity-making-my-brain-spin-help.8873/