Teleporting on the Path

Hello,
I want to calculate a line from point A to Point B. But instead of starting from point A, I want my object to start at the 30% completed on this line.

With the attached picture as example,
Blue Line - Original Calculated Line
Red Line - 30% of this line (or any other percentage game will calculate)
Yellow Circle - The point I want to spawn my object
Green Line - The line I want my object to follow

As you can guess, Blue line is easy but the rest is the part I’m stuck on. I tried my chance with AstarPathGetNearest (Vector3.Lerp(originalStartPosition,originalTargetPosition) ) yet my agents got ended up in an un-movable location from time to time. Any advice?

I want to tell you that I’m not an expert and probably there is a better way to do it. But in case someone struggles with the same problem in the future here is what I did.

Below is as an example for referance. I put no comments, hopefully it’s understandable.

float targetProgress = 0.3f;
Path path = ABPath.Construct(originStartLocation(blueLine), originFinishLocation(blueLine));
AstarPath.StartPath(path);
path.BlockUntilCalculated();
List< ’ Vector3 ’ > myPath = path.vectorPath;
int totalPathVector = myPath.Count();
int targetVectorIndex = (int)Math.Round(totalPathVector * targetProgress,0);
Vector3 targetVector = myPath[targetVectorIndex]; (yellow circle location)
var targetNodeLocation = AstarPath.active.GetNearest(targetVector, NNConstraint.Default); // Probably not needed but just in case.
gameObjectToSpawn.transform.position = targetNodeLocation;

By the way, my object already has an AIDestinationSetter component in it which is already pointed at the game object at the target location. So wherever I spawn it, it will start moving towards it.

1 Like