Fixing my code to make a raycast always point up

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By yatoimtop

I have this mechanic where a player can toss a coin and teleport to it, and it’s basically just a small rigid body that has a raycaster node on it to it to sense if anything is above it.
I’ve been using this to point it up:

raycast.look_at(coin.translation,Vector3.UP)

That code is placed in the process function of the coin, and it mostly works except that the raycast tries to point to somewhere near the origin, resulting in the raycast never perfectly pointing upwards. I’ve tried a variety of things but I can’t get this one line of code to work.

Is there a better solution or some kind of helpful godot function to make this work? I’ve scoured the docs to no avail.

Use the global_position of the coin and add to that distance to look at

$raycast.look_at(coin.global_transform.origin + Vector3(0, 5, 0), Vector3.UP)

If your coin never rotates then there’s no reason to use this code at all

Wakatta | 2021-01-18 04:19

the coin does rotate! i tried your code real quick with visible collision shapes and the raycast rotates with the coin. i just figured out a solution, but thank you for your response!

so my code creates a 3d position in the code and then updates it to be above the coin at all times, then i use the cast to function to point at the 3d position node

3Dpos.translation=coin.translation+Vector3(0,3,0)
raycast.cast_to=to_local(3Dpos.translation)

yatoimtop | 2021-01-18 07:18