0 votes

Hi, what code should I write so that when my player enters the enemy raycast range, my player is killed and the game starts again?

 extends KinematicBody

const MOVE_SPEED = 4
const MOUSE_SENS = 0.5

onready var anim_player = $AnimationPlayer
onready var raycast = $RayCast

func _ready():
    yield(get_tree(), "idle_frame")
    get_tree().call_group("zombies", "set_player", self)

func _input(event):
    if event is InputEventMouseMotion:
        rotation_degrees.y -= MOUSE_SENS * event.relative.x

func _process(delta):
    if Input.is_action_pressed("exit"):
    if Input.is_action_pressed("restart"):

func _physics_process(delta):
    var move_vec = Vector3()
    if Input.is_action_pressed("move_forwards"):
        move_vec.z -= 1
    if Input.is_action_pressed("move_backwards"):
        move_vec.z += 1
    if Input.is_action_pressed("move_left"):
        move_vec.x -= 1
    if Input.is_action_pressed("move_right"):
        move_vec.x += 1
    move_vec = move_vec.normalized()
    move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
    move_and_collide(move_vec * MOVE_SPEED * delta)

    if Input.is_action_pressed("shoot") and !anim_player.is_playing():
        var coll = raycast.get_collider()
        if raycast.is_colliding() and coll.has_method("kill"):

func kill():

This my player script

extends KinematicBody

const MOVE_SPEED = 3

onready var raycast = $RayCast
onready var anim_player = $AnimationPlayer

var player = null
var dead = false

func _ready():

func _physics_process(delta):
    if dead:
    if player == null:

    var vec_to_player = player.translation - translation
    vec_to_player = vec_to_player.normalized()
    raycast.cast_to = vec_to_player * 1.5

    move_and_collide(vec_to_player * MOVE_SPEED * delta)

    if raycast.is_colliding():
        var coll = raycast.get_collider()
        if coll != null and coll.name == "Player":

func kill():
    dead = true
    $CollisionShape.disabled = true

func set_player(p):
    player = p

This my zombie script

in Engine by (30 points)

I'm not seeing any problem in your scripts. It could be a collision layer issue, or it could be that the raycast's collide_with_bodies property has been set to false.

What is the problem exactly? Is there nothing happening when zombies get close to the player?

Could you post which collision layers and masks you've set on the player, zombies and raycasts? Could you also check that your raycasts have collide_with_bodies equal to true in the inspector?

1 Answer

0 votes

Your script is fine except for this part

var coll = raycast.get_collider()
    if raycast.is_colliding() and coll.has_method("kill"):

Like in the zombie script you need to check if is_colliding is true before get_collider or else it will throw an error return null.

if raycast.is_colliding()
    var coll = raycast.get_collider()
    if coll.has_method("kill"):
by (3,223 points)
edited by


get_collider() returns null if is_colliding() is false. His script won't throw since he checks is_colliding() before using coll.

You're right, I was thinking along the lines of intersect_ray() which throws an error if it returns null and is used regardless.

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.