GetNeareast will pass through the locked grid


#1

When I use the GetNearest method to find a position that can be moved, if two grids are connected locked, it will still pass through the intersection of the two grids.

I am using a joystick to operate the unit.

before
1

after
2

How should I use it to avoid this problem?


#2

Hi

Could you explain again? How do you use GetNearest to move the agent? Are you using any pathfinding at all?
Are you using tags to block the movement?


#3

Hello, I didn’t use pathfinding, use the joystick operation unit to move directly, yes, I used the TAG to mark the locked grid.

_constraint = NNConstraint.Default;
_constraint.distanceXZ = true;
_constraint.constrainTags = true;
_constraint.tags = (int) WorldTag.Building;

and use

Vector2d clampedPos = AstarPath.active.GetNearest ( nextPos, _constraint ).position;

Because the position returned by the GetNearest method is not the same as the distance from the unit position, I have length-scaled the returned position to ensure that the speed does not change when the obstacle is reached. So I feel the reason here, let the unit enter the locked area.


#4

Hi

Well, just calling GetNearest will not do anything to validate that an agent could actually move between those two points.
I would recommend that you use the Linecast method instead.
See https://arongranberg.com/astar/docs/gridgraph.html#Linecast3


#5

Ok,thanks,
Do I need to add Collider obstacles to use this method? Or just set the Tag of the grid and pass it directly to the start and end points.


#6

Hi,
I found that use Linecast can only on walkabke=false, So how does it work on the grid that uses Tag?

The second is how to use the method to move the unit on the edge of the obstacle. If the unit reaches the obstacle, I only find the GraphHitInfo.point. If the speed is 5, the distance from the start point to the hitPoint is 2, and the remaining 3, how? It can be moved according to the direction of the movement to continue the remaining distance on the edge 3


#7

Hi

There is currently unfortunately no variant of Linecast that filters on a tag. You can get sort of the same information by using the overload that outputs all traversed nodes to the trace variable. (see https://arongranberg.com/astar/documentation/4_1_16_7f164ebc/gridgraph.html#Linecast4), and then check if any of those nodes contain that tag. Is there any reason you use tags and not walkability for this?


#8

Yes, there are 2 sides players, one can through the grid, and other side can’t, so I use the Tag