Intersect ray for enemy sight is absolutely cooked (non-functional)

Godot Version

v4.2.2

Question

-The goobers in question: Player, Enemy (moves towards player)
-What isn’t happening: I’m trying to make a raycast to check if an object is between the enemy and player, from the enemy’s script, for the enemy to detect the player.

So I’ve tried a ton of different ways of casting an intersect ray but it never works. When I stopped getting errors the cast would output at “random” (but consistent) position somewhere near world origin (but not at world origin). I finally got it to actually output objects, but it would output a seemingly random output at a particular position (When both were in a pretty small area a bit positive on x and z, it would output a “random” one of the walls I had there, but nothing was between player and enemy.). Something being between player and enemy had no effect on the output. I just need an intersect ray that works with two moving objects, to check if anything is between them

pain
image

Can you paste your script for altering the ray’s target position?

Well I had it just set the var in the physics loop so it just remade the raycast each physics frame, but I redid the code so much I dunno, I also thought I could use a shapecast (Which I can’t because of how that works) so I deleted the code. So yeah I just need any way to get the raycast to work, because I can’t find an answer on how to. (Note the raycast is code based, not node based)

It’s hard to know what went wrong. A common issue with raycasts is assuming it’s target_position property is in global space. It actually checks collisions pointing in target_position’s direction and length. So properly setting this every frame would look like this:

extends RayCast3D
@export var target: Node3D

func _physics_process(delta: float) -> void:
    var diff = target.global_position - self.global_position
    target_position = diff

I reconstructed my code btw
image
So are you saying “to” should be “player.position - position” instead?

Using direct_space_state.intersect_ray you probably just want to use global_position instead of position

1 Like

When I did that it started consistently returning values pertaining to the player, but I can’t get it to check if it isn’t the player (it’s actually outputting the objects it collides with now, but as a weird string with a lot in it now)
image
That above is the output from “print(result.get(n))” and below is the statement with error
image
How do I get the object in dictionary rather than the raycast converting the object to this weird string?

for _ in Dictionary gives you the keys in a dictionary, this result one is full of strings.

Try indexing something useful directly:

if result:
    var position: Vector3 = result["position"]
    var collider: Object = result["collider"]

Here’s the docs, listing the rest of keys to make use of.

1 Like

Ok, I got it retuning an object, and when I start the project it returns the player!
Great! Solved, right?
I step back like 2 (meters?) and it starts detecting the ground. The ground is not between me and the enemy, and also, I checked by renaming the ground and I saw the name in the “print()”
I think it’s casting down?

1 Like

Where is the center of your player? Where is the center of your enemy? If their origin is at their feet then the ray will also be at their feet.

1 Like

Oh shoot yeah thanks that would do it :smiley:

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