x


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 :)

more ▼

asked Jun 25 '12 at 01:54 AM

asimov gravatar image

asimov
157 19 36 40

Sounds ok to me. You're talking about waypoints for the position AND for the look at right ?

Jun 25 '12 at 03:02 AM Berenger

Yes, that's correct. So am I right in thinking that the only adjustment the camera has to make is in its rotation, if its constantly moving in its forward direction? For the camera to correctly LookAt() the Markers, their rotation values do not matter right, just as long as they are positioned in the centre of the curve all the way along it?

If you can possibly help me out with some of the code for the task that would be awesome, as I'm not too sure how to put this together at the moment.

For the first part I've described above I'm imagining a List called say, 'SectionRailMarkerList' which takes (Adds) all the tube's Rail Markers as it approaches it. As a starting point, how would you recommend I achieve this?

Thanks

Jun 25 '12 at 08:49 AM asimov
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Jun 25 '12 at 02:03 PM

filler03 gravatar image

filler03
117 15 23 30

Thanks for the suggestions.

Well, although the tubes do generate at random (there will be 7 different shapes), the rail markers for each can be positioned inside each of the 7 tubes and saved as a prefab. This way the rail markers for say, tube 1, will be in the same position (locally) the next time it's selected. These can be stored in a List inside a script attached to each tube, which can then be passed to the camera as it enters that tube. It can then move through each element in the List and LookAt() each of them as it approaches. That's the theory anyway, putting it into practice is where I think I'll need help.

Well, what I am trying to avoid are things like using Find(), Instantiate() and Destroy() too often, as these are computationally expensive and can lead to bad performance. However, if this idea doesn't work then I'll try anything :)

Jun 25 '12 at 02:23 PM asimov
(comments are locked)
10|3000 characters needed characters left

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;
}
more ▼

answered Jun 25 '12 at 02:42 PM

Loius gravatar image

Loius
10.7k 1 11 41

Hi Vincenti, thanks for that.

The reason I'm storing them in a List is so that their positions can simply be looked up by the camera, instead of finding them as it moves.

BTW, I was under the impression that the camera would only have to change its rotation and move in its forward direction. So LookAt() the marker and move forward, not change its position as in your MoveTowardsNextMarker() function?

Thanks

Jun 25 '12 at 03:27 PM asimov

If your rail markers don't move, it's sufficient to just rotate once then move along the camera's .forward, yes. I tend to build things far more extensible and complex than necessary so I end up with my pathing objects moving around sometimes (giant robot with circling fighter planes, ha).

The code I posted up there ^ is using an existing list, rather than recreating the same list in the camera. You certainly -can- rebuild the list in the camera object, I'm just not seeing the advantage to it if the list already exists in your tube objects.

If "GetNextTube" isn't a trivial function, then I could see how rebuilding the list would be desirable - in that case, I'd recommend that whenever a tube is created, you add its markers to the "top" of the camera's list, and have your camera navigate towards the "bottom" marker on the list, then pop that marker off once it gets in position.

Jun 25 '12 at 07:42 PM Loius
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 25 '12 at 05:42 AM

filler03 gravatar image

filler03
117 15 23 30

Hi Joe,

So you mean look for (Find()) the Rail Markers in real-time as the camera is moving? If so, (and I could be wrong here) this could be quite expensive compared to having the positions already stored in the list and then simply looking the next one up.

If anyone else can confirm this?

Jun 25 '12 at 08:53 AM asimov
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3015
x329
x316
x240
x12

asked: Jun 25 '12 at 01:54 AM

Seen: 523 times

Last Updated: Jun 25 '12 at 07:42 PM