I'm trying to code an enemy to do certain actions when they're looking at the player. I'm making a vision script for the enemy by making a collision cone to detect when the player is in front of them, and then do a raycast to look at the player. It's mostly working, with one problem - it doesn't follow the player up and down, i.e. when the player jumps. Here is the code:
func sight() -> void:
var overlaps = $visioncone.get_overlapping_bodies()
player_present = false
if overlaps.size() > 0:
for overlap in overlaps:
if overlap.name == "Player":
playerposition = overlap.global_position
print(playerposition)
player_present = true
if player_present:
$visionraycast.look_at(playerposition)
$visionraycast.force_raycast_update()
if $visionraycast.is_colliding():
var collider = $visionraycast.get_collider()
print(collider.name)
A couple of weird things are happening. First, when I print the playerposition, it is returning an accurate 3D vector (including a changing y value when I jump). Also, when I view the raycast in the debug options, it does follow the players feet in 3D (i.e. it looks down when the player is closer to the enemy and at a more horizontal angle when further away). However, when I jump, the raycast remains planted in the floor below the player. Even weirder, when I jump, the collider.name returns the wall behind the player rather than the floor! I think the problem is with look_at, and I have tried inserting the Vector3.UP as an argument and it doesn't change any behaviour. Any help appreciated, thank you!
I see, thank you. I already have the raycast target position as 0, 0, -100 in the inspector, and I have tried passing $visionraycast.look_at(playerposition,Vector3.FORWARD) and $visionraycast.look_at(playerposition,Vector3.RIGHT) and still getting the same results. Then I tried $visionraycast.rotate_x(PI) before calling look_at and still got the same result, so Iâm not entirely sure how to achieve what youâre describing. Sorry if Iâm being stupid, Iâm quite new to this.
Iâve had the debug display on the whole time and itâs behaviour has been consistent throughout - it looks at the player but seems unable to look above y=0 (relative to the raycast position).
And $visionraycast.rotate(Vector3.UP,PI) followed by $visionraycast.rotate(Vector3.RIGHT,PI) doesnât change anything, eitherâŚ
Also, you should break the loop when player is found. If something else is in the cone and it gets iterated over after the player, its position will override the player position previously stored in playerposition
If something else is in the cone and it gets iterated over after the player, its position will override the player position previously stored in playerposition
I might be missing something, but due to the âifâ statement, that would only happen if the other object is named âPlayerâ, wouldnât it? I have tested this by running behind obstacles etc. and that part of the code seems fine, the raycast continuously tracks the player and returns the accurate playerposition, even with several other objects in the cone.
OK so after doing a lot more testing and messing around it seems that the actual problem is that the raycast is lagging behind the player⌠It is able to look up, but for some reason it doesnât always follow the player perfectly. This is weird as it should be updating every frame, right? So how is it possible that itâs not keeping up? I would guess the issue is to do with what order the game executes the player script and the raycast script. The enemy is lower down on the scene tree so I would have thought it performs the raycast AFTER the player has moved, but obviously that is not the case. Iâm going to mark this as solved since the issue is not what I thought it was (canât seem to delete the post), but thank you everyone for your help and sorry to have wasted your time!
What do I do if I realise the problem isnât really a problem then? I canât delete the topic and I donât want to leave it unsolved so other people donât waste time on it.