- Home /
Object following List of Vector3 coordinates
Hello, I have a list of points that represent coordinates at given time, each point is one second 'away' from the previous one. I used a Catmull-Rom script to smooth out the curve. Im having trouble with switching to next point once the object reaches current target point.
I started from a waypoint following script from here, posted by phodges. I modified it to suit my needs, and was left with following:
void Update()
{
if (TimeKeeper.timePassed == AARInit.planeList[IDnumber].startingTime)
hasStarted = true;
if (hasStarted)
{
relative = GetTargetPoint() - dron.transform.position;
direction = Quaternion.LookRotation(relative);
distanceToTarget = transform.InverseTransformPoint(GetTargetPoint());
if (distanceToTarget.magnitude < arrivalRadius&& distanceToTarget.magnitude > 0)
SelectNextTarget();
dron.transform.rotation = direction;
dron.transform.position += dron.transform.forward * speed * Time.deltaTime;
}
}
void SelectNextTarget()
{
nextTarget++;
if (nextTarget < AARInit.planeList[IDnumber].waypointsCR.Count - 1)
target = AARInit.planeList[IDnumber].waypointsCR[nextTarget];
else
Destroy(dron);
speed = ((target - dron.transform.position).magnitude) * AARInit.crSamples;
}
Vector3 GetTargetPoint()
{
return target;
}
The problem is in vector3 relative. Sometime the object will pass targeted waypoint and it will point in the oposite direction of wanted motion (it will look at the target point witch is now behind it). If the step during a frame is larger than arrivalRadius the object will just 'twitch' around target point never being able to get close enough to switch to the next one.
I can solve this by increasing arrivalRadius, but that creates a problem when the points are close together. It causes the object to move much faster than it should.
Is this good direction Im heading regarding moving objects along predefined path? Or is there some easier way?
I tried to get relative.magnitude to be negative once the target is passed using transform.InverseTransformPoint(), but nothing... Im stumped, and asking for any help.
Answer by Vodonik H · Jul 28, 2013 at 01:54 PM
Nevermind, I solved it myself. Added this at the end of SelectNextTarget(); So simple, yet it made me insane. :)
arrivalRadius = (speed* Time.deltaTime);
Your answer
Follow this Question
Related Questions
Waypoint System 1 Answer
array using getcomponents (c#) 3 Answers
Raycast with tags problem, resultless raycast.. 1 Answer
Waypoint tree structure 2 Answers