0 votes

Hello, i am a newbie at Godot and i am making a 2D top-down shooter. I was implementing a sight mechanic for the enemies, where the enemy is supposed to raycast toward the player when it enters his range, and if the raycast is not interrupted by any walls, the player is "seen" and they start chasing, but i've ran into a where the Raycast2D never returns the player, only "TileMap". This is what the code currently looks like:

extends "res://AI.gd"

onready var line = $Line2D

var hitting_player: bool = false
var player_in_range
var player_in_sight
var first_time = true

func run():
    sight_Check()

    if player_in_sight:
        var direction = body.global_position.direction_to(player.global_position)
        direction *= actor_values.speed
        body.move_and_slide(direction)

    if hitting_player:
        player.handle_hit(actor_values.strength)

func _on_Area2D_body_entered(body):
    if body == player:
        hitting_player = true

func _on_Area2D_body_exited(body):
    if body == player:
        hitting_player = false

func _on_Vision_body_entered(body):
    if body.is_in_group("Player"):
        player_in_range = true
        player = body
        print("player in range")

func _on_Vision_body_exited(body):
    if body.is_in_group("Player"):
        player_in_range = false
        print("player out of range")

func sight_Check():
    if player_in_range:
        var space_state = get_world_2d().direct_space_state
        var sight_check = space_state.intersect_ray(position, player.position, [body], body.collision_mask)
        if sight_check:
            if first_time:
                line.add_point(body.global_position)
                line.add_point(sight_check.collider.global_position)
                first_time = false

                print("spotted" + sight_check.collider.name)
            if sight_check.collider.is_in_group("Player"):
                print("player in sight")
                player_in_sight = true
            else:
                player_in_sight = false

The code works just fine when i delete all collision layers from the TileMap, but when i put them again, the enemies don't detect the player even if he's not behind a wall. While debugging, print("spotted" + sight_check.collider.name), only returns "spottedTileMap" no matter what, and the lines i draw point to positions that i don't understand where they came from, and i have no idea on what could be wrong now.

Godot version Godot_v3.2.3-stable_mono_win64
in Engine by (32 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.