0 votes

for some reason my mob sight only become true when my player at top him and false when have something in between player and mob. if go to left/right/bottom mob, i did't get any print , but if put something in between i will get false print. did i do right?

mob code

onready var player = get_parent().get_node("player")
var player_in_range
var player_in_sight


func _physics_process(delta: float) -> void:
    sight_update()
func _on_sight_body_entered(body: Node) -> void:
    if body == player:
        player_in_range = true
        print(self.name," player in range ", player_in_range)


func _on_sight_body_exited(body: Node) -> void:
    if body == player:
        player_in_range = false
        print(self.name ," player out of range ", player_in_range)

func sight_update():
    if player_in_range == true:
        var space_state = get_world_2d().direct_space_state
        var sight_check = space_state.intersect_ray(position, player.position, [self], collision_mask)
        if sight_check:
            if sight_check.collider.name == "player":
                player_in_sight = true
                $icon.modulate = ColorN("black",1)
                print(self.name," player in sight = ", player_in_sight, " = ",sight_check.position)
            else:
                player_in_sight = false
                $icon.modulate = ColorN("crimson",1)
                print(self.name," player not in sight = ", player_in_sight, " = ",sight_check.position)

map node
enter image description here

in Engine by (350 points)

1 Answer

0 votes

Unless you want to have obstacles/other targets to block abilities, I would probably use an Area2D node to check whether your player is in range of an enemy. The Raycast.get_collider() function returns only the first object that it collides with.

With an Area2D node you can use the get_overlapping_bodies() function to get all bodies within your a certain distance of your enemy then check to see if one of them is your player. Or set the collision bits in such a way that enemies can only detect players and vise versa.

by (2,098 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.