0 votes

For example say the player is behind a wall (this is only left and right movement)
i want the raycast to go infinitely until it hits a wall if it does it stops but if it hits player it goes with the player till its out of visibility

Godot version 3.3.3 stable
in Engine by (27 points)

1 Answer

0 votes

Raycast can;t be infinite. if Raycast is infinite, then time needed to create Raycast is infinity.

1 Set RayCast2D,cast_to to size of your level.
2 Create separate Collision Layers for Player and walls
3. Remove Player from RayCast2D.collision_mask if first collision is wall, and add Player back if first collision is not wall.

by (846 points)

Could you elaborate a bit more on that and maybe give an example of code I'l elaborate a bit i want the ray cast to go until it hits a static body and not past that

That is how it works by default. You do not need to set anything except Raycast2D.cast_to. You can set it in editor. To detect if Raycast2D hit any CollisionShape2D just check is_colliding()

Example:

extends RayCast2D


func _physics_process(delta: float) -> void:
    if is_colliding():
        print("hit")

I don't need help with colliding I just don't want it detecting what is behind the walls

enter image description here

see the scorpion detected the player behind the wall and shot how do i make it so it doesn't do that

It already forks like this by default. You do not need to do anything for that.

https://youtu.be/04A7pUkhx3E

Ok so what do i need to change then here is all my script

this is Player

  extends KinematicBody2D


signal health(health)
signal max_health(health)

var velocity = Vector2()
var on_ground = false
var is_attacking = false
var is_dead = false
var fail_safe = false
var is_left = false
var is_right = false
var health_setup = false

const FLOOR = Vector2(0, -1)
const SPEED = 500
const GRAVITY = 30
const JUMP_POWER = -700
const FIREBALL = preload("res://Fireball.tscn")
const STILL = 0

export(float) var max_health = 100

onready var health = max_health
onready var invulnerbillity_timer = $AnimationPlayer


func _process(_delta):
    if health_setup == false:
        emit_signal("max_health", max_health)
        emit_signal("health", max_health)
        health_setup = true
        print("hi")

func _physics_process(_delta):
    if fail_safe == false:
        if is_dead == false:
            if Input.is_action_pressed("move_right"):
                if is_attacking == false || is_on_floor() == false && is_dead == false:
                    is_left = false
                    is_right = true
                    velocity.x = SPEED
                    if is_attacking == false:
                        $AnimatedSprite.flip_h = false
                        if on_ground != true:
                            pass
                        else:
                            $AnimatedSprite.play("walk")
                        if sign($Position2D.position.x) == -1:
                            $Position2D.position.x *= -1
            elif Input.is_action_pressed("move_left"):
                if is_attacking == false || is_on_floor() == false && is_dead == false:
                    is_right = false
                    is_left = true
                    velocity.x = -SPEED
                    if is_attacking == false:
                        $AnimatedSprite.flip_h = true
                        if on_ground != true:
                            pass
                        else:
                            $AnimatedSprite.play("walk")
                        if sign($Position2D.position.x) == 1:
                            $Position2D.position.x *= -1
            else:
                velocity.x = 0
                if on_ground == true && is_attacking == false:
                    $AnimatedSprite.play("idle")


            if Input.is_action_just_pressed("my_action") && is_attacking == false:
                is_attacking = true
                $AnimatedSprite.play("attack")
                var fireball = FIREBALL.instance()
                if sign($Position2D.position.x) == 1:
                    fireball.set_fireball_direction(1)
                else:
                    fireball.set_fireball_direction(-1)
                get_parent().add_child(fireball)
                fireball.position = $Position2D.global_position



            if Input.is_action_pressed("jump"):
                if is_attacking == false:
                    if on_ground == true:
                            velocity.y = JUMP_POWER
                            on_ground = false


            velocity.y += GRAVITY

            if is_on_floor():
                if on_ground == false:
                    is_attacking = false
                on_ground = true
            else:
                if is_attacking == false:
                    on_ground = false
                    if velocity.y < 0:
                        $AnimatedSprite.play("jump")

            velocity = move_and_slide(velocity, FLOOR)

            if get_slide_count() > 0:
                for i in range(get_slide_count()):
                    if "Enemy" in get_slide_collision(i).collider.name:
                        dead()
                    if "scorpion" in get_slide_collision(i).collider.name:
                        dead()

            if health < 100:
                if Input.is_action_just_pressed("test_spam_heal"):
                    health += 25
                    emit_signal("health", health)




func dead():
    if $invulnerbillityTimer.is_stopped():
        health -= 25
        emit_signal("health", health)
        on_hit()
        if health <= 0:
            $Control.visible = false
            fail_safe = true
            velocity = Vector2(0, 0)
            $CollisionShape2D.set_deferred("disabled", true)
            $AnimatedSprite.play("death")
            $Timer.start()


func on_hit():
    if health > 0:
        $CollisionShape2D2.set_deferred("disabled", false)
        $CollisionShape2D.set_deferred("disabled", true)
        invulnerbillity_timer.play("hurt")
        invulnerbillity_timer.queue("invulnerbillity")
        $invulnerbillityTimer.start()



func _on_AnimatedSprite_animation_finished():
    is_attacking = false

func _on_Timer_timeout():
    get_tree().quit()


func _on_invulnerbillityTimer_timeout():
    invulnerbillity_timer.play("rest")
    $CollisionShape2D2.disabled = true
    $CollisionShape2D.set_deferred("disabled", false)

this is Scorpion

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() && is_attacking == false:
                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

and i have no singletons or global variables

Also I tried making a ray cast script but it didn't work that's why its in the scorpion script

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.