This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

This is all the code:

extends Node

export var fire_rate = 0.5
export var clip_size = 5
export var reload_rate = 1

onready var raycast = $"../Head/Camera/RayCast"
var current_ammo = 0
var can_fire = true
var reloading = false

func _process(delta):
    if Input.is_action_just_pressed("primary_fire") and can_fire:
        #fire the weapon
        if current_ammo > 0 and not reloading:
            print("Fired Weapon")
            can_fire = false
            current_ammo -= 1
            check_collision()
            yield(get_tree().create_timer(fire_rate), "timeout")

            can_fire = true
        elif not reloading:
            print("Reloading")
            reloading = true
            yield(get_tree().create_timer(fire_rate), "timeout")
            current_ammo = clip_size
            reloading = false
            print("Reload complete")

func check_collision():
    if raycast.is_colliding():
        var collider = raycast.get_collider()
        if collider.is_in_group("Enemies"):
            collider.queue_free()
            print("Killed " + collider.name)

But only the last part:

func checkcollision():
if raycast.is
colliding():
var collider = raycast.getcollider()
if collider.is
ingroup("Enemies"):
collider.queue
free()
print("Killed " + collider.name)

When i start the debug and when i shoot it dont detect it and do not print anything

in Engine by (17 points)

1 Answer

0 votes

You need to check inputs in func _input(event):, not func _process(delta):. Or poll the Input singleton in _physics_process(delta):.

Check this link for good examples: Input examples

by (283 points)

P.S. you might want to check raycast in _physics_process(delta):

This is the video:

You don't want to use copied code if you don't understand how it works. Otherwise, something will go wrong and you will be sitting there wasting a bunch of time scratching your head, and you won't be able to incorporate what you learned in the future. I am not going to debug a third party's code snippets so you can move on with it in the interim, because I don't think you will benefit from that.

If my answer is confusing or badly worded, ask specific clarifying questions.

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.