Godot 4.2.2rc1, Scripts written in C#
my "UpdateLos" functions basically loops through a list of Vector2 items, sets the the TargetPosition of a RayCast2D node to the value of the current Vector2, forecupdates the RayCast2D and then checks if there is a collision (with one of the collision layers of the tiles on a TileMap).
If a collision is detected, I want the position where the collision has happened.
My problem now is, that I always get the same result for the collision point, no matter what I set as the "recast.TargetPosition". For example, if I aim at the top left of my tilemap, I get bottom right as collision point. If I aim at bottom left I get bottom right as well as answer.
The UpdateLos function is being called from a script, that is attached to the tilemap.
Any help is appreciated...
Jens
public static List<Vector2> UpdateLos(
Vector2 mapPlayerPosition,
RayCast2D losRayCast,
TileMap visTileMap
)
{
_npcs = Data.ActiveMapNpcs;
List<Vector2> markedTiles = new List<Vector2>();
var targetPointList = CreateTargetPoints(Data.Visibility);
foreach (var mapTargetPoint in targetPointList)
{
var los = AddLine(visTileMap, false);
var pointOrigin = visTileMap.MapToLocal((Vector2I)Data.CurrentPlayerPos);
var pointTarget = visTileMap.MapToLocal((Vector2I)Data.CurrentPlayerPos + (Vector2I)mapTargetPoint);
losRayCast.TargetPosition = pointTarget;
losRayCast.ForceRaycastUpdate();
los.Points = new[] { pointOrigin, pointTarget};
GD.Print("aiming at: " + pointTarget);
if (losRayCast.IsColliding())
{
los.DefaultColor = new Color(1, 0, 0);
//
losRayCast.ForceRaycastUpdate();
var localCollisionPoint = losRayCast.GetCollisionPoint();
var mapCollisionPoint = visTileMap.LocalToMap(localCollisionPoint);
GD.Print("collision detect at: " + localCollisionPoint);
//
var newPath = CreateAStarPath(mapPlayerPosition, mapCollisionPoint, visTileMap);
ClearPath(newPath.ToList(), visTileMap, markedTiles); ;
}
else
{
var mapRayCastEndpoint = mapPlayerPosition + mapTargetPoint;
var newPath = CreateAStarPath(mapPlayerPosition, mapRayCastEndpoint, visTileMap);
ClearPath(newPath.ToList(), visTileMap, markedTiles);
}
}