Raycast's TargetPosition points in a nonsense direction

Godot Version

v4.2.2.stable.mono.official [15073afe3]

Question

I’m sure the answer is very obvious but I’m having a hard time figuring things out. I started working on my AI for my game, and the detection system I made has two parts. First, it’s a large Area2D which will detect when the player enters it, and once they did, it will start a timer which times out every 0.25 seconds, updating the TargetPosition property of a RayCast2D, which should stop as soon as it hits a wall, but report when it hits the player without anything in it’s way. This works, however, the TargetPosition does not. I will attach a screenshot of what happens. This happens in every case, if I just use _player.Position, or _player.GlobalPosition, or _player.GlobalTransform.Origin. Any help is very much appreciated!

1 Like

Since the TargerPosition is relative to the Raycast’s position, the solution is to subtract the position of the raycast from the target position, so this works:

_raycast.TargetPosition = _player.GlobalPosition - _raycast.GlobalPosition;

In case anyone is looking for this solution in the future.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.