Linecast to a point outside of map returned the center of the map instead of the edge

Sorry for my english.

I use gridgraph. What i want is when player clicked on a point which isn’t on the map, the character will move towards that point and stop at the edge of the map. So i use the following method to send a linecast.

bool isBlocked = graph.Linecast((Vector3)start, (Vector3)toEnd, null, out hit);
and use the hit’s position.

i expected that hit would return the edge of the map, but instead, it gives me the center.

UPDATE:
It will work if there is an unwalkable area at the edge.

The linecast will check if it hit any obstacle. If it did not hit an obstacle I think it will just set the hit point to Vector3.zero.

That should be the default behavior. You do not need to use a linecast for this. By default when requesting a path to a point the path will go to the closest node to the requested one which can be reached from the start point of the path.
You may have to increase the setting A* Inspector -> Settings -> Max Nearest Node Distance as the search for the closest node (mentioned above) will abort if it cannot find a node within that distance.

Thanks very much.

I thought the linecast is the same as unity’s NevMesh.Raycast. Will you plan to make some changes to linecast? I thinks returning the vector3.zero is a little bit counter-intuitive

I don’t think max nearest node distance fits us. I’ll make some changes to my code, and perhaps surround the whole map with obstacles.

Yeah, you are right that it might make sense to return the original target point as hit.point. I will make a note of it.

1 Like

[quote=“aron_granberg, post:4, topic:4159, full:true”]
Yeah, you are right that it might make sense to return the original target point as hit.point. I will make a note of it.
[/qu

Thanks. Will it be possible to return the point where the linecast the the edge intersected?

That should just be the hit.point field, shouldn’t it?

But when there’s no collider, it returns vector.zero.

So which edge are you saying it is intersecting?
Screenshot maybe?