Monster free sight to player

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

in 3D space!
Monster detect player with Area/CollisionPolygon
I want to check if monster has free sight and not a wall between the monster and the player!
I try use “RayCast.cast_to”
an example I am trying:
$RayCast.cast _ to = transform.xform(player.transform.origin)
WRONG WRONG WRONG!

Someone know a easy way to see if something is in between player and the Monster?
underscore don’t show right: cast underscore to “cast_to”

:bust_in_silhouette: Reply From: Magso
transform.xform(player.transform.origin)

Isn’t right but neither would be

transform.basis.xform(player.transform.origin)

Because that would give you the players position as if it is a child of monster.

You need to take the monster’s position away from the player’s position.

player.transform.origin - transform.origin

Thanks for give me confidence in: player.transform.origin - transform.origin

I end up with this $RayCast.cast_to:
$RayCast.cast_to = Vector3(transform.origin.x - player.transform.origin.x, player.transform.origin.z - transform.origin.z, player.transform.origin.y - transform.origin.y + 0.1)

I sit and do one value: X, Y; Z at a time and Debug / Visible Collision Shapes On

my gravity is:
var gravity = Vector3.DOWN * 12
Have I somehow turn my world to Vector3(X, Z, Y)
My $RayCast.cast_to formula make me confused, but it works!

MOBii | 2020-12-08 13:45

Grrr
I stop the Mob to walking and turning!
When I start make Monster start turn again the $RayCast.castto start go wildly on my screen again!
It’s something with the Monster direction that also somehow is in the calculation too!

MOBii | 2020-12-08 14:06

Have I somehow turn my world to Vector3(X, Z, Y)

Have you imported a model as the top scene node and parented everything else under it?
In Godot you need to specify an initial node for the scene before anything else, I typically use Node for maps, Node2D or Spatial for anything with a specific custom behaviour and Control is for UI.

Magso | 2020-12-08 14:21

Finally was more easy than expected!
$RayCast.cast_to = $RayCast.to_local(player.transform.origin)

Thanks all for taking time try help me!

MOBii | 2020-12-08 17:30