Make camera follow cylindrical track

What I have is a very long, curved cylinder that I want to act like a track would in a racing game. The camera moves with player input, and I want to restrict it to being able to move only around the cylinder (and of course forward).

I was thinking I could use this code, which I found in a default unity script MouseOrbit:

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);

But I’m having difficulty figuring out exactly what to do.

Also the camera has to orient itself correctly (so it’s not upside down when it’s on the bottom of the cylinder). But I think I can figure that one out.

You have two separate technical problems here. The major one is how to get the camera to follow the cylinder. It is possible to directly walk the cylinder using raycasting, but a much better and more reliable method is to create a path down the axis of the cylinder and walk walk the path. One free path solutions is iTween. At the site you can find both iTween and iTween Visual Path Editor. You would likely use iTween.PutOnPath() to walk the path.

As for your rotation issue. I would setup your system as follows. I would use an empty game object and iTween.PutOnPath to follow the path. I would add an second empty game object offset from the original one as a child. Transform.Rotate() on the original empty game object would rotate the second game object around the cylinder. Finally I’d have the camera track the position but not the rotation of the second empty game object. The camera would not be a child. That would keep the camera rotation so that it always faced “up”.