I have a raycast on an enemy which I want to cast to the player's position. Since the cast_to coordinates are in local space and I need the players global position, I'm trying to find out how to convert the player's global position to the raycast's local space. I just don't know how to do that. I've found out about the to_global() and to_local() functions, but they don't have a clear way to use them to assign a value.

I've tried things like: $RayCast.cast_to = $RayCast.to_local(target) $RayCast.cast_to = target.to_local($RayCast.cast_to) Just as experiments, really, but nothing seems to work.

How do I take the player's position and convert it into the raycast's local space using these functions?

There might be a built in function, but you can just subtract the enemy position from the player position and I think that will then be in relative space. For example:

var local_player = player.position - enemy.position

For 2D (or use player.translation for 3D)

If your target position is in global space then your first line should work. Otherwise you need to first transform it to global space and then to raycast's local space.

24 days later

I figured it out! You can set the cast_to property of a raycast like this:

var raycast = [Raycast node reference]
raycast.cast_to = to_local(player.translation)

I know this is probably obvious to some, but I only ever saw this function with a node reference attached to the front, so I didn't know it could be used without. Thanks to those who offered support!

@bennnperson said: I figured it out! You can set the cast_to property of a raycast like this:

var raycast = [Raycast node reference]
raycast.cast_to = to_local(player.translation)

I know this is probably obvious to some, but I only ever saw this function with a node reference attached to the front, so I didn't know it could be used without. Thanks to those who offered support!

You can't call a method without node (or object) reference "in front of it". This has it too, it's just not explicitly stated. Node you're calling it on is the node the script is attached to. So writing: to_local(player.translation) is in fact shorthand for: self.to_local(player.translation)

a year later