Function Agent.SetTarget
Point towards which the agent should move.
void SetTarget (
targetPoint | Target point in world space. |
|
float | desiredSpeed | Desired speed of the agent. In world units per second. The agent will try to move with this speed if possible. |
float | maxSpeed | Max speed of the agent. In world units per second. If necessary (for example if another agent is on a collision trajectory towards this agent) the agent can move at this speed. Should be at least as high as desiredSpeed, but it is recommended to use a slightly higher value than desiredSpeed (for example desiredSpeed*1.2). |
endOfPath | Point in world space which is the agent's final desired destination on the navmesh. This is typically the end of the path the agent is following. May be set to (+inf,+inf,+inf) to mark the agent as not having a well defined end of path. If this is set, multiple agents with roughly the same end of path will crowd more naturally around this point. They will be able to realize that they cannot get closer if there are many agents trying to get closer to the same destination and then stop. |
Point towards which the agent should move.
Usually you set this once per frame. The agent will try move as close to the target point as possible. Will take effect at the next simulation step.
The system assumes that the agent will stop when it reaches the target point so if you just want to move the agent in a particular direction, make sure that you set the target point a good distance in front of the character as otherwise the system may not avoid colisions that well. What would happen is that the system (in simplified terms) would think that the agents would stop before the collision and thus it wouldn't slow down or change course. See the image below. In the image the desiredSpeed is the length of the blue arrow and the target point is the point where the black arrows point to. In the upper case the agent does not avoid the red agent (you can assume that the red agent has a very small velocity for simplicity) while in the lower case it does.
If you are following a path a good way to pick the target point is to set it to targetPoint = directionToNextWaypoint.normalized * remainingPathDistance
Where remainingPathDistance is the distance until the character would reach the end of the path. This works well because at the end of the path the direction to the next waypoint will just be the direction to the last point on the path and remainingPathDistance will be the distance to the last point in the path, so targetPoint will be set to simply the last point in the path. However when remainingPathDistance is large the target point will be so far away that the agent will essentially be told to move in a particular direction, which is precisely what we want.