0 votes

hello all i am trying to make my enemy attack after every 0.5 seconds but it doesnt instead it attacks really fast and constantly hence i want to use a timer and fix this. i tried using a timer and writing code on appropriate place but it did not work out i also saw godot documentation but it did not help much.

if u can help find another error here is that it doesnt detect the player until player hits him and then it works as it should but still attacks fast.

i want to know where to write the timer code in this:

here is the enemy code( i have cut and made it small and relevant):

var player_in = false

func idle_state(delta):
    velocity = velocity.move_toward(Vector2.ZERO, friction * delta)

func attack_state(delta):
    animationstate.travel("attack")  #animation tree is used
    velocity = velocity.move_toward(Vector2.ZERO, friction * delta)

func attack_animation_finished():
    state = idle

func seek_player():
    if playerdetectionzone.can_see_player() and player_in == true:
        state = attack
    elif playerdetectionzone.can_see_player() and player_in == false:
        state = walk

func _on_attackradius_body_entered(body):
    player_in = true
    state = attack

func _on_attackradius_body_exited(body):
    player_in = false
    state = walk
in Engine by (28 points)

Where do you start the timer in code?

1 Answer

+3 votes

Where you want to use the timer, add a timer node, and do this:


Change the number to any number that you want to use.
So you must know how long the animations are.

Then, go into the timer, click the tab in the top right that says "Node" and select "timeout()".

Add that to your script, it'll say:

func _on_Timer_timeout():

So there you write what you want to do when the timer ends.

If you want multiple different things, (not from the same timer) then add as many timers as you need. Do the steps mentioned above and you'll be fine!

by (101 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 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.