Camera to smoothly LookAt() a series of empty objects along a curved path that act as a 'rail'

Hi,

I am trying to think up various methods to enable a camera to change its rotation as it moves through a curved tube, and always look in the tube’s forward direction. One of the methods that I’m considering is to distribute a series of empty objects (we’ll call them ‘Rail Markers’ for this explanation) along the curve and have the camera LookAt() each of them as it approaches them, one at a time. I presume the position of the camera would take care of itself as long as it was always moving in its forward direction after rotating the correct amount.

I’d like some advice on how to store the Rail Markers as the camera moves through the tube. There are a number of tubes of various shapes that generate ahead of the Player during the game and at random. With this is mind, I can store the positions of the Rail Markers as part of each tube’s prefab, but how would I write a script to have the camera look for the ‘next’ Rail Marker as its inside a tube.

I can imagine a script that says, as the the camera is about to enter a new tube;

  • copy its Rail Markers into a List (which should hold their positions/rotations inside the tube)

  • Lerp/Slerp to each Rail Marker when a certain distance from it

  • Remove all elements in the list when exiting the tube

I’m not sure whether that makes sense, as there are probably a number of steps missing.

Any suggestions or guidance would be greatly appreciated :slight_smile:

You could use a static variable that keeps track of how many waypoints you have passed so far, then when each tube spawns you could use gameObject.Find to find the waypoints in the new tube and rename them to include the static number and then increment it, and use the same variable to tell the camera script where to look.

Being that the tubes generate at random how were u planning on having the rail markers position already stored?

Another thing that might work would be to have a “currentTube” GameObject variable on your camera script, and set it equal to your Instantiate function and then access the waypoints throuth this variable? I’m just theowing out ideas here it might not be the most efficient way of doing it but it just came to mind that it might work.

I’m not sure why you want a list of railmarkers to traverse if you’re just s/lerping to the next one -

If you’re already storing the markers with the tube objects via a script - i’m guessing something like

var marker : Transform;

Then your camera can just do something like

var curMarker : int;
var curTube : Tube;
function FindNextMarker() {
  curMarker++;
  if ( curMarker >= curTube.marker.length ) {
    curTube = GetNextTube();
    curMarker = 0;
  }
}

function Update() {
  MoveTowardsNextMarker();
  if ( CloseToMarker ) FindNextMarker();
}

function MoveTowardsNextMarker() {
  transform.position += (curTube.marker[curMarker].position - transform.position).normalized * speed * Time.deltaTime;
}