4.3
Hi , I been coding in Godot for 3 months now(still kinda an noob ).
Im trying to figure out how i can add an lunging attack for my enemy, but i can not think of an way to add it in .
here’s my code
func _physics_process(_delta: float) -> void:
if first_defence :
if chase_player :
var direction = (player.position - position).normalized()
velocity = direction * speed
else :
velocity = Vector2(0, 0)
if lunge:
velocity = player.position * speed * _delta
please help
try using a tween or an animation to move the enemy.
you can queue a number of actions and play them from a function that runs once.
this would not be in physics_process but you would have to create a state where nothing happens so it can do its thing.
const LUNGE_SPEED : float = 10.0
enum {IDLE, CHASE_PLAYER, LUNGE}
var state : int = 0
var lunge : bool = true
func _physics_process(_delta : float) -> void:
match state:
LUNGE:
move_and_slide()
CHASE_PLAYER:
var direction = (player.position - position).normalized()
velocity = direction * speed
move_and_slide()
IDLE:
move_and_slide()
use an Area2D to detect if the player is within lounging distance and trigger the tween there and change the state. then change the state back:
func _on_area_2d_body_entered(body):
if lunge:
state = LUNGE#lunge state
lunge = false#deplete it and wait to get it back
var tween : Tween = create_tween()
tween.tween_property(self, "velocity", -body.position, 0.1)#back a bit
tween.tween_property(self, "velocity", body.position * LUNGE_SPEED, 0.5)#lunge forward
tween.parallel().tween_property(self, state, CHASE_PLAYER, 0.6)#go back to chasing player
tween.tween_property(self, "lunge", true, 2.0)#can lunge again in 2 seconds
I hope this works, let me know if it doesn’t.
edit: on second though, it would be a better idea to use a tween_callback to change state since it’s an int and it could lead to a bug. for now it should work.
2 Likes
thanks for helping me but its not working for me , i don’t know what to put in the brackets for tween_callback (the tween.parallel din’t work ) , also for some reason the enemy doesn’t show up .
tween callback calls a function. you need to define a function and call it there. then it will run at that point in the queue.
func change_state(st : int) -> void:
state = st
then call it like:
tween.tween_callback(change_state.bind(CHASE_PLAYER))#we pass an argument to the function with bind
parallel makes it so that the tween is played at the same time as the previous one, they start at the same time.
but in this case we don’t need to wait, we want to change state immediately after the last tween is played.
is it throwing any errors?
I typed this in the forum, made it in the spot from the code you posted.
if anything is not working correctly and it’s not throwing errors, it could be the way it calculates where to go.
oh, you need to set state to CHASE_PLAYER somewhere. doing so from some node that detects when a player gets close, like an Area2D would be a good idea.
in this script I also do not define player or speed. I don’t have a context of the rest of your script or how you handled things.
I imagined that the enemy was a CharacterBody2D and you need to add and connect an Area2D
to the function on_area_2d_etc()
.
thanks so much I’d learned a lot , the enemy not showing up was on my part .