Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

In a scenario where the player moves freely along the walls of a tunnel (like this), what is the best way to move the player along the walls of the tunnel?

I'm using Unity3d game engine. I've tried to remove all gravity and apply relative downward force on the object on every tick. Pressing right or left key applies relative force on the x-axis. But the player object will not rotate along with the walls of the tunnel and hence wont climb. I can calculate tangent to the center of the circle inside the tunnel and rotate the player myself but that wont work with curved tunnels.

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You could do a Physics.Raycastin the down direction of the player. The normal of the RaycastHit this gives you, can then be used to determine the new orientation of the player. Then to "stick" the player to the surface, the new position can be calculated from the hit point, the surface normal and the desired distance from the surface.

For smoother results, do this twice, once a bit left of the player and once a bit to the right, then average.

This should work even when the tunnel cross section is not circular, should you choose so at some point.

share|improve this answer
add comment

The cross section of the part of the tunnel the player is in is always a circle. Simply use the center point of the tunnel as a position and the direction the tunnel is going as a rotation axis for rotating your player. Essentially, if the camera is exactly following the centerline of the tunnel, you'll rotate the player around the Z axis of the camera.

You may be able to achieve this by setting the origin of the player above the player by the radius of the tunnel. Then simply move the player along the center of the tunnel, and rotate about the player Z axis. Or make a parent object called "tunnelfollower" and put the player as a child object, offset with the tunnel radius.

enter image description here

That'll take care of curves in the tunnel and you won't be dealing with gravity. This rotation doesn't change the rotation of the player relative to the center of the tunnel. This means relative to the tunnel walls, the player up is aligned to the normal.

share|improve this answer
    
I don't understand how I can calculate the center point of the cross-sectional circle the player is in at any moment, and the z-rotation of that circle. The tunnels would be curved. What I have at any moment is the player position (origin) and the origin of the tunnel. I'm new to game dev so I might be missing something obvious. –  Arsalan00 Aug 13 '13 at 19:21
add comment

I think the best solution is to turn off physics. Make it work in not curved tunnels, then use a vertex shader to create the curves. If you use the same transformation both in tunnels and the player, you achieve the effect you desire.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.