# How to move around a player in a curved tunnel?

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.

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.

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.

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.

• 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. – Arsalan Ahmad Aug 13 '13 at 19:21

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.