0 votes

Ok so i cant figure out how to make it so the raycast sees the wall but doesn't do anything with it. i have all the collision layers and stuff up i just need it to say when it collides with a tilemap do nothing and just stop it at that tilemap but when the player walks in front of it shoot

here is my code for the enemy

extends KinematicBody2D

var velocity = Vector2()
var direction = 1
var is_dead = false
var is_hurt = false
var attack_anim = false
var health_check = false
var rng = RandomNumberGenerator.new()
var stun = false
var walk_cancel = false
var is_attacking = false
var one_shot = false
var is_left = false
var is_right = false

const GRAVITY = 10
const FLOOR = Vector2(0, -1)

export(int) var speed = 100
export (float) var max_health = 200


onready var health = max_health


const FIREBALL = preload("res://scorpion fireball.tscn")





func dead():
    rng.randomize()
    var my_random_number = rng.randi_range(0, 10.0)
    if my_random_number >= 10:
        health -= 50
        stun = true
    elif my_random_number <= 0:
        health -= 0
    else:
        health -= 25
    health_bar2()
    on_hit()
    if health <= 0:
        $Sprite.visible = false
        is_dead = true
        velocity = Vector2(0, 0)
        $CollisionShape2D.set_deferred("disabled", true)
        $AnimatedSprite.play("dead")
        $Timer.start()

func on_hit():
    if health > 0:
        $CollisionShape2D2.set_deferred("disabled", false)
        $CollisionShape2D.set_deferred("disabled", true)
        $CollisionShape2D3.set_deferred("disabled", true)
        if stun == true:
            attack_anim = true
        walk_cancel = true
        $AnimatedSprite.play("hurt")
        $Timer2.start()

func _physics_process(_delta):
    $RayCast2D2.set_collision_mask(2)
    if is_attacking == false:
        if is_dead == false:
            if attack_anim == false:
                velocity.x = speed * direction
            velocity.y += GRAVITY

            if attack_anim == false:
                if direction == 1:
                    if is_right == false:
                        $RayCast2D2.cast_to *= -1
                        $Position2D.position.x *= -1
                        $CollisionShape2D.set_deferred("disabled", true)
                        $CollisionShape2D3.set_deferred("disabled", false)
                    $AnimatedSprite.flip_h = true
                    is_left = false
                    is_right = true
                elif direction == -1:
                    if is_left == false:
                        $RayCast2D2.cast_to *= -1
                        $Position2D.position.x *= -1
                        $CollisionShape2D3.set_deferred("disabled", true)
                        $CollisionShape2D.set_deferred("disabled", false)
                    $AnimatedSprite.flip_h = false
                    is_left = true
                    is_right = false
            if attack_anim == false:
                if walk_cancel == false:
                    $AnimatedSprite.play("walk")

            velocity = move_and_slide(velocity, FLOOR)

            if is_on_wall():
                direction = direction * -1
                $RayCast2D.position.x *= -1
                $Sprite.position.x *= -1

            if get_slide_count() > 0:
                for i in range (get_slide_count()):
                    if "Player" in get_slide_collision(i).collider.name:
                        attack_anim = true
                        $AnimatedSprite.play("attack")
                        get_slide_collision(i).collider.dead()
                        $Timer3.start()

            if $RayCast2D.is_colliding() == false:
                direction *= -1
                $RayCast2D.position.x *= -1
                $Sprite.position.x *= -1





            if $RayCast2D2.is_colliding():
                var collider = $RayCast2D2.get_collider()
                print(collider)
                is_attacking = true
                $AnimatedSprite.play("long attack")
                var fireball = FIREBALL.instance()
                if $AnimatedSprite.flip_h == false:
                    fireball.direction *= -1
                    fireball.set_fireball_direction(-1)
                else:
                    fireball.set_fireball_direction(1)
                get_node("Position2D").add_child(fireball)
                $Timer4.start()

func _on_Timer_timeout():
    queue_free()

func _on_Timer2_timeout():
    is_dead = false
    stun = false
    attack_anim = false
    walk_cancel = false
    $CollisionShape2D2.set_deferred("disabled", true)
    if is_left == true:
        $CollisionShape2D.set_deferred("disabled", false)
    if is_right == true:
        $CollisionShape2D3.set_deferred("disabled", false)

func _on_Timer3_timeout():
    attack_anim = false

func health_bar2():
    if max_health == 200:
        if health == 200:
            $Sprite/AnimationPlayer.play("full_health")
        if health == 175:
            $Sprite/AnimationPlayer.play("87%health")
        if health == 150:
            $Sprite/AnimationPlayer.play("75%health")
        if health == 125:
            $Sprite/AnimationPlayer.play("63%health")
        if health == 100:
            $Sprite/AnimationPlayer.play("50%health")
        if health == 75:
            $Sprite/AnimationPlayer.play("37%health")
        if health == 50:
            $Sprite/AnimationPlayer.play("25%health")
        if health == 25:
            $Sprite/AnimationPlayer.play("13%health")
    if max_health == 150:
        if health == 150:
            $Sprite/AnimationPlayer.play("full_health")
        if health == 125:
            $Sprite/AnimationPlayer.play("87%health")
        if health == 100:
            $Sprite/AnimationPlayer.play("63%health")
        if health == 75:
            $Sprite/AnimationPlayer.play("50%health")
        if health == 50:
            $Sprite/AnimationPlayer.play("37%health")
        if health == 25:
            $Sprite/AnimationPlayer.play("13%health")
    if max_health == 100:
        if health == 100:
            $Sprite/AnimationPlayer.play("full_health")
        if health == 75:
            $Sprite/AnimationPlayer.play("75%health")
        if health == 50:
            $Sprite/AnimationPlayer.play("50%health")
        if health == 25:
            $Sprite/AnimationPlayer.play("25%health")

func _on_Area2D_body_entered(_body):
    if get_slide_count() > 0:
        for i in range (get_slide_count()):
            if "Player" in get_slide_collision(i).collider.name:
                attack_anim = true
                $AnimatedSprite.play("attack")
                get_slide_collision(i).collider.dead()




func _on_Timer4_timeout():
    is_attacking = false
Godot version 3.3.3 stable
in Engine by (27 points)

1 Answer

0 votes

It's better just to put the code relevant to your question.

If I understand you right you want the raycast to collide with a wall / tilemap but to branch on colliding with the player. So, as you say, you need to set up the masks and layers as desired, then just:

func _physics_process(delta):
    if $RayCast.is_colliding:
        if $RayCast.get_collider.name == "Player":
            #do stuff
by (813 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.