So i make raycasts from every step of the path way on a hexagonal grid to the endpoint of the path to avoid weird looking direct paths with navmesh. So, i cast a ray near obstacles, properly assigned a correct layer, use a layermask that only detects obstacles and... It acts super weird. Basically it encounters walls that aren't there while ignoring the walls that ARE there. I am rotating prefabs before spawning them, maybe that has something to do with it? I'm frankly stumped and have no idea what i'm doing wrong by now. In pictures, red lines are casts that result in a collision with an obstacle, while blue ones show a cast that does not touch an obstacle. Any help will be appreciated.
#Mesh collider and raycasts
1 messages · Page 1 of 1 (latest)
share your code?
And explain maybe in this image which parts are colliders and of what kind?
For what specifically?
the code that is doing the raycasts that are acting weirdly and the code that is drawing this blue/red line
just, the whole thing ideally
Here's the code for raycasts
foreach (HexCoordinates hexCoordinates in hexCoordinatesList)
{
if (Physics.Raycast(levelGrid.GetRelativeWorldCoordinates(hexCoordinates) + Vector3.up,
levelGrid.GetRelativeWorldCoordinates(hexCoordinatesList[hexCoordinatesList.Count - 1]) + Vector3.up,
Vector3.Distance(levelGrid.GetRelativeWorldCoordinates(hexCoordinates) + Vector3.up,
levelGrid.GetRelativeWorldCoordinates(hexCoordinatesList[hexCoordinatesList.Count - 1]) + Vector3.up),
characterMovementParameters.obstacleLayerMask))
{
Debug.DrawLine(levelGrid.GetRelativeWorldCoordinates(hexCoordinates) + Vector3.up,
levelGrid.GetRelativeWorldCoordinates(hexCoordinatesList[hexCoordinatesList.Count - 1]) + Vector3.up,Color.red,10);
Move(hexCoordinates);
}
else
{
Debug.DrawLine(levelGrid.GetRelativeWorldCoordinates(hexCoordinates) + Vector3.up,
levelGrid.GetRelativeWorldCoordinates(hexCoordinatesList[hexCoordinatesList.Count - 1]) + Vector3.up, Color.blue,10);
Move(hexCoordinates);
Move(hexCoordinatesList[hexCoordinatesList.Count - 1]);
break;
}
}
isMoving = true;
characterMovementParameters.navMeshAgent.SetDestination(movementWaypoints[0]);
Basically it sends a cast from every coordinate on the grid into the final point of the path
And if it finds no obstacles, it tells the character to move directly to the final point
Your raycast is wrong
Physics.Raycast(levelGrid.GetRelativeWorldCoordinates(hexCoordinates) + Vector3.up,
levelGrid.GetRelativeWorldCoordinates(hexCoordinatesList[hexCoordinatesList.Count - 1]) + Vector3.up```
raycast takes a position vector and a direction vector
DrawLine takes two position vectors
if you want the raycast to behave like DrawLine, you should use Physics.Linecast
not Raycast
Ooooooh i see!
Are there any downsides of using linecast instead of raycast? For example, would it be faster to do a linecast between 2 points or a raycast with proper direction vector and ray length limit?
they are exactly the same
just two different ways of specifying the ray/line to cast
Ah, i see