Search Unity

Crawling on surfaces

Discussion in 'Scripting' started by AgentParsec, Sep 17, 2014.

  1. AgentParsec

    AgentParsec

    Joined:
    May 5, 2012
    Posts:
    403
    What's the best way to have someone grab hold to a surface and crawl along it in a game where the player has other physics going on at the same time (gravity and such), essentially ignoring physics for a moment until the player lets go of the wall? I figure it would have something to do with making the object temporarily kinematic, but then how do you keep it able to move along the surface and not let go of it until you release?

    Is there some universal way to do this, or do I have to use some kind of invisible guide object that would need to be attached to every surface?
     
  2. AgentParsec

    AgentParsec

    Joined:
    May 5, 2012
    Posts:
    403
    Still trying to find the best way to accomplish this. I know people have made ninja-esque games before where you can jump and grab hold of a wall and crawl along it and such. I'm just trying to figure out how people are coding that, because I haven't found a reliable way to do it yet.
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    I recall a big thread on this topic about a year ago. If I remember correctly, the best solution at the time was to turn off gravity on the character, raycast "down" (relative to the character's/wall's orientation), and apply a downward Y velocity (again relative to the orientation). I can't find that thread now, but I see a couple people posted their code on unityAnswers:

    http://answers.unity3d.com/questions/468330/character-that-climbs-everything-including-on-ceil.html
    http://answers.unity3d.com/questions/155907/basic-movement-walking-on-walls.html
     
  4. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    that's pretty much what i would have suggested. can you give us more detail on the circumstances under which you need this effect to operate? is it like Link climbing vine walls, or more like Faith running on walls? the way this mechanic is used in the game greatly impacts its implementation.
     
  5. AgentParsec

    AgentParsec

    Joined:
    May 5, 2012
    Posts:
    403
    Sorry for the very late reply.

    Actually, it's for a game that's already in zero gravity. The character will be jetting around in zero g, with his momentum carrying him along. He will need to be able to grab hold of (some) fixed surfaces to stop his momentum, and can crawl along them if desired. The gameplay is restricted to a 2D plane, so there's no need to make it function in 3D or from a first person perspective.

    Ideally, I'd like it so that the character attaches himself to the surface and can move up and down along it until he reaches its edge, at which point he stops and will not continue crawling off of the surface. From there, my plan is that once grabbing hold of a surface, the character can be rotated by 90 degrees to either hold onto it in a parallel crawling fashion (arms and legs on the surface, body parallel to the surface), or in one of two perpendicular grips (either as hands only with the feet pointed away, or braced with the hands and feet in a crouch against the surface). He can crawl only when in the parallel positions, but in all positions he can push himself away from the wall at different strengths depending on the position. Here's a very rough diagram (sorry for the use of MS Paint):



    In the above diagram, the character would be able to crawl in either of the two side positions by pressing up or down. In the other two positions, he would be stationary. Pressing left and right would rotate him between each position. (Imagine it not as a box in the center of the diagram, but that it is one surface, and the camera is rotating to the player's new orientation.)

    I also want to implement a similar system that lets him grab hold of free-floating debris (boxes, rocks and such), though that will function differently since it's not a fixed surface.
     
  6. AgentParsec

    AgentParsec

    Joined:
    May 5, 2012
    Posts:
    403
    I should also note that everything above with the diagram and the act of pushing off from the surface is secondary to the thing that I most need help with, and that's the actual process of sticking to the surface and crawling along it.
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    It's pretty simple. Whenever you touch an object, update the collision normal. The normal can be used to determine the correct angle of push or rotated gravity to keep the player on the object. Alternatively, you can use this normal to fire a ray, and then position the player on the object.