Search Unity

Localized Gravity in a curved tube

Discussion in 'Physics' started by Star_Kaster, Nov 3, 2018.

  1. Star_Kaster


    Sep 2, 2017
    Hi Guys, I got a bit of a weird one, in the simplistic form, I want to be able to walk on walls.

    I have a curved tube in which the player can move around, but I need the player to be able to walk on all sides of the tube as though any side is always down from the players perspective. I've attached a picture which hopefully describes the outcome I am aiming to get to. Gravity pushes outwards from the center of the pipe allowing you to walk upside down if you want.

    I have tried a combination of things like moving the tube around the player, but that doesn't work when you add additional characters to the world in the same tube. I tried now to do a sort of reversed faux gravity where there are little "planets" in the center of segmants in the tube, but that results in some really crazy dancing when the player is in the influence of more than 2 spheres at once. Not to mention the rotation that is completely off. My only other way I can think of is to get the pipe to suck the player to the mesh collider, but I don't know where to start looking if such a thing exists.

    I have exhausted all of my brain power to try find a solution, so here I am.
    Please help. And thanks already!
  2. SparrowsNest


    Apr 6, 2017
    1) make global gravity 0/ tick off useGravity on the rigidbody

    2)for each characters look at the surface normal, lock transform.up to it and give it gravity on the opposite of the normal

    3) profit
    xorpheous likes this.
  3. Star_Kaster


    Sep 2, 2017
    Thanks for the help! okay okay, I was at least on the right track in my mind.. I just didn't know the right terms. I was thinking of having transforms in the middle of the pipe at each of the mesh segmants, and then somehow finding out the direction from that center and the feet of the character, that way you would be able to apply gravity in that direction. Its possibly still a reasonable solution maybe?

    However looking at your suggestion and now knowing the terms, I don't know how to make use of it. Perhaps I am a bit way over my head with this. But I think its still worth learning. I don't suppose you know of any tutorial that could help point me in the right direction? If even vaguely relevant, knowledge is still knowledge.

    Right now, I am not sure where to get the surface normal, I assume it comes from the generated meshs calculated normals, but then how would you know which normal in the list to use? That's the problem I had with my attempt at putting transforms in the middle. I couldn't figure out how to tell which one to look at since the character should be able to walk anywhere within the pipe.
  4. Matt1000


    Dec 16, 2016
    I actually already did a mechanism like this one. Pretty much how Mario galaxy works. I'll break it down for you.

    A plane's normal is a perpendicular vector to that plane. In other words is an "up" line from any surface. To calculate it you need to calculate the cross product of two vectors that are from the plane. The cross product is not a problem since you can simply do Vector2.Cross() and that it. You'll need two vectors parallel to the surface NOT parallel between them though.
    It's really not that hard once you wrap your head around it.

    To calculate a vector parallel to a surface you can simply use two points from that surface. You just do P2 - P1. and you'll get a vector from P1 towards P2. If you have another point you can do P3 - P1 and you'll have your two vectors. It's very important not to use three points in a row otherwise both vectors will be the same... Generally, P1 can be the point the player/object is on and if P2 is in front then P3 would be to the side (in front and the back won't work.)
    If you are over curve objects you CANNOT use the point below you, since it will lead to the normal being a little off. If that's the case One vector needs to be P1 - P2 (P1 being in front of you and P2 to your back) and the other P3 - P4 (P3 to your right and P4 to your left). That will be enough to even the surface.

    Once you have normal you just need to do a little trick. This normal can actually be 2 different vectors, might be the vector you need or the opposite one, in which case your object will start floating away. Just do a check from your object towards the vector that you got and if you hit something, then it's the right one.
    If you get the "up" vector, check if there is something. Since there won't be anything, use the other one.

    all these points need to be really near to you. Otherwise, it may not work very well. And many times this mechanism has problems with lowpoly objects.
    After all that you can create extra things like stabilizers but that is the basis. Might seem hard but it a lot of simple things really.
    Hope it helps ;)
  5. Antypodish


    Apr 29, 2014