How to flip raycast in Godot 3?

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

I have player scene with a KinematicBody2D as the parent node and a Raycast2D as a child. The player can only shoot if the enemy is within reach of the raycast. The problem I’m having is that the gun will shoot an enemy even if the player is not facing that enemy (!!!)

How can I either (1) make the raycast flip with the player; or (2) disable the raycast if flip_h is true (not ideal)

I’m also open to idea of shooting only if the enemy is within the camera view, with or without raycast detection.

Project is in Godot 3 stable.

“The player can only shoot if the enemy is within reach of the raycast.”
Is the raycast rotating 360° (ike radar) or how does this assumption work in your code? How you determine if the enemy is in reach? With a collision area around the player?

" The problem I’m having is that the gun will shoot an enemy even if the player is not facing that enemy"
How you determine if the player is facing the enemy?

Would be easier to help you if you would provide some code segments.

rolfpancake | 2018-02-20 12:16

:bust_in_silhouette: Reply From: rolfpancake

Despite from my comments which aim at making your question more understandable I would like to answer your two mentioned points:

How to make the raycast flip with the player
I assume: raycast.position = player.position or it is a child of the player and therefore the rays position is set to Vector2(0,0). If so you only need to point the raycasts cast_to-vector to the players direction-vector (and multiply it with the length of your field of view). This depends on the direction your player views when it is initialised. In platformers (like Mario) this would be Vector2(1,0) because initially the player looks in positive-x-direction. When moving the player the raycast should be updated. If the player is rotated the raycasts cast_to-vector should be rotated accordingly. This can be achieved with something like this:

ray.cast_to = Vector2(1,0).rotated(deg2rad(player.rotation_degrees))

You can also ask if your player is flipped when updating the ray. Like so:

if player.get_node("sprite").flip_h:
    ray.cast_to.x *= -1

How to make the raycast flip with the player

Deactivating a ray is also no problem:

ray.enabled = false

But I don’t know if this would help in your situation. I would add a direction-of-sight-vector for your player and update it when the player is moved and depending on this the ray should be updated.

You can also achieve an enemy detection with an area which has a polygon-collisionshape modelling the players field-of-view. If the enemy touches the area the enemy is in reach for shooting and also the player looks into the right direction - no need for a raycast. This is a typical line-of-sight problem which can be solved with different approaches.

Thank you!!! I followed your directions and sure enough, they worked!

koigx | 2018-02-20 18:19