- Home /
Camera wil not move between given points, stays at first given point
I'm putting together a "raid cam" or "overview cam" for a given map in my shooter for an observing player to use to fly down a given preset path (to prevent griefing and cheating, this may change in my final build)
Unfortunately though I can get the program to invoke the given method, iterate through each of my points, and display this to the debug console, my player camera does not move from the very first start point. I'm attempting to use the Vector3.MoveTowards function at the moment but if there is a better way to move the camera between the points I am open to that.
The code is as follows, and heavily commented for clarity on my thought process:
// Invoked from external function, takes precoded path and translates player between the given points
public void FlyThroughPath(List<Vector3> path){
Debug.Log ("FlyThroughPath");
// If the flightpath is not empty
if(path.Count > 0){
// Enable the fly through (Externally tracked)
enabled = true;
// Sets the start point as the first point in the track
StartPoint = path[0];
Debug.Log ("Start point is set as " + StartPoint.ToString());
// moves the player immediately to this start point
transform.position = StartPoint;
// For each point in the given path we'll get the current position of the camera and the next point
foreach (Vector3 point in FlightPath){
// The first point move is redundant as the player is already at the start point
// Get the current position for the start point
StartPoint = transform.position;
// The end point is our given point in the FlightPath
EndPoint = point;
// StartTime = Time.time; // Not used at the moment
// Move controller between points
// Invoked to move between the given points
FlyToNextPoint(StartPoint, EndPoint);
}
// When done we disable the fly through (Externally tracked not used yet)
enabled = false;
}
}
// Takes two points and transforms the player camera between them at a preset speed
public void FlyToNextPoint(Vector3 start, Vector3 end){
Debug.Log ("FlyToNextPoint");
// Grab the time for movement speed and duration
// Snag the player's last chosen speed for consistency
m_speed = flyMotor.m_actualSpeed;
// put together the time we want to use between the two points
float t = m_speed * Time.deltaTime;
// Move the player between the given points at the given speed
Debug.Log ("Flying to " + end.ToString() + " from position " + start.ToString());
// Move the player camera towards the next point from the given start point
transform.position = Vector3.MoveTowards(start,end, t);
}
Answer by Eudaimonium · Sep 10, 2015 at 12:40 PM
You appear to be making the same mistake I just commented an hour back on different question,
Basically you have a logic for traversing the entire path within your own while/for/foreach loop. This results in entire logic for traversal (and the traversal itself) to happen in a single frame. When that frame gets rendered, your player/camera is already at the end of the path.
Remove the foreach loop, and implement the interpolation through the points logic using the Update loop, and move in small increments per each pass of Update loop (each frame) to actually have the object move.
Refer to my answer here: http://answers.unity3d.com/questions/1064238/unity-crashes-on-event-of-specific-code.html - just change the logic a bit so that when distance to your destination (the point within your list of locations) is smaller than threshold - you have arrived - instead of stopping movement, just change the destination to the next point.
If you need that solved in code, post it on Pastebin and let me know, I'll help you out.