- Home /
How can I determine the nearest point to a position (for pathfinding)?
Hi
I'm working with the Unity pathfinding system in a game where the player can order an agent to move to a destination by tapping a point in the screen. The problem is that if the player taps a position too close to another agent, they will start colliding and jittering.
What I'm trying to do is to calculate a new destination for the agent that is close to the destination set by the player but with enough distance so they don't collide. My problem is that I have very little knowledge of vectors so I tried to get a direction by substracting the destination requested and the position of the moving agent and then move from that point a pair of units (the radius of the agents, actually) but the result was nothing like I was expecting (please, see the attached picture to better understand what I'm trying to do).
So my question is what changes should I do to this code in order to get the desired destination (shown in the picture)?
newDestination = (agent1.transform.position - destinationSetByPlayer);
newDestination = destinationSetByPlayer + (newDestination * agentRadius);
Answer by fafase · Nov 25, 2012 at 09:51 AM
My guess, use linecast. There maybe other solutions and even some better ones but here is mine.Add a sphere collider to your agent as trigger. Get the click position and draw a linecast from the player to that position. http://docs.unity3d.com/Documentation/ScriptReference/Physics.Linecast.html
Now, using the info from the linecast, if you get a collision it means you are inside the sphere collider, you get the collision with the hit variable and use it as target vector. If you get no collision tehn you use the tap position. See the pictures. The red circle is the sphere collider.
Remember to add a layer mask for your guy so that the line does not collide with himself. The sphere radius will define the min distance with your agent.
But the pathfinding system may walk red Agent1 up, over and down, putting the blue "have to stop here" X more to the northwest of the red X (in other words, the picture sometimes wrong.)
Can still use this method, but need to shoot from the 2nd to last waypoint to the last (does pathfinding still return an array of waypoints?)
Seems like a nice alternate solution, if I take into account what O. Reynolds said I could be able to implement it (thanks to both of you).
I still wish I could calculate the position though, I'm pretty sure it can be done but my ignorance of vectors gets in the way (I'll give it a try and if it fails, I'm doing this). Thanks.
Answer by Owen-Reynolds · Nov 26, 2012 at 03:13 PM
IMHO, unask your question. Just let them click anywhere and don't try to fix it at that stage. Maybe the blocker will move, or maybe someone will wander in your path midway.
Instead, as they move, use standard "raycast to not ram stuff." That generally includes stuff like "sidestep trees" and "close enough to target, stop here."
But I'm trying to avoid making too much raycasts (for performance). Anyway, if the other agent moves I think I can "update" the destination of the moving agent (I have an object that keeps status of the current locations and destinations of the agents so that should help). If someone wander in the path midway, the obstacle avoidance system should solve it... I think.