- Home /
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 :)
Sounds ok to me. You're talking about waypoints for the position AND for the look at right ?
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 $$anonymous$$arkers, 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, 'SectionRail$$anonymous$$arkerList' which takes (Adds) all the tube's Rail $$anonymous$$arkers as it approaches it. As a starting point, how would you recommend I achieve this?
Thanks
Answer by filler03 · Jun 25, 2012 at 05:42 AM
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.
Hi Joe,
So you mean look for (Find()) the Rail $$anonymous$$arkers 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?
Answer by filler03 · Jun 25, 2012 at 02:03 PM
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.
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 :)
Answer by Loius · Jun 25, 2012 at 02:42 PM
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;
}
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, ins$$anonymous$$d 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 $$anonymous$$oveTowardsNext$$anonymous$$arker() function?
Thanks
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.
Your answer
Follow this Question
Related Questions
Can anyone please tell me why this script doesnt work? - Raycasting 1 Answer
Making a camera that follows a rigidbodied sphere. 2 Answers
Camera following/looking at aircraft 1 Answer
Camera Follow Object While Looking At Other Target 2 Answers
Camera position on path Proportional to Player'sposition 0 Answers