Hello, pretty new to GDScript and Godot. Using RC1 of Godot 4 currently.
I have an enemy that follows a path, but when it spots you it will go after you. Now I've been trying to implement knockback (once I attack the enemy) but I can't get anything to work, and I assume it's because of how I do the navigation movement. Here's the code where move_and_slide() is called:

func MoveTowardsPoint(delta, speed):
	var targetPos = nav_agent.get_next_path_position()
	var direction = global_position.direction_to(targetPos)
	faceDirection(targetPos)
	velocity = direction * speed
	move_and_slide()
	if playerInSightClose:
		CheckForPlayer()
		print("Werkin Close")
	if playerInSightFar:
		CheckForPlayer()
		print("Werkin Far too yay")

Ang here is how a piece from shrtsword.gd script which handles how I deal damage using the weapon... for the moment at least:

func _on_hitbox_body_entered(body):
	if body.is_in_group("Enemy"):
		$AttackAudio.stream = random_snd_attack[randi() % len(random_snd_attack)]
		$AttackAudio.play()
		print("enemy hit")
		body.take_damage(10)

The sword has a three hit combo set up too above this part. If I could get the knockback going generally, I will redo it later to happen on the third hit. But I digress. I Would appreciate at least some tips on how to handle my problem. Thank you.

  • There's no need to re-write the agent code at all in the case of relying on physics for the knockback no. What I would do is set the agent location to be itself in order to stop the agent's movement, then disable the agent node entirely while you want the physics and then have the physics code execute after that. Even if the agent gets moved, especially with Godot 4, if it bounces quite far off you should be able to re-enable the agent and then have it continue on a path if that's what you want, hope that makes sense.

    This is purely pseudo code but as an example.

    if agentIsTouchingPlayer == true:
    agent.set_target_position(agent.global_transform.origin) // or just global_transform.orgin depending on where you have your movement code.
    
    // physics stuff afterwards

How would you like this knockback implemented? Physics based? Or something more maths based? You could set the navigation position target to something behind the enemies' current location and then just increase the speed temporarily to give it a little knockback effect however if you wanted something physics based I would stop the agent movement altogether and enable the physics.

    Lethn
    I would actually prefer physics, especially since down the line I want to have player get knocked back as well enemies depending on how much force was applied (for example if you're dashing into them while attacking, or jumping down on them from somewhere). It is (supposed to be) an action first person game with melee/ranged combat mix. And by "stop the agent movement altogether" do you mean I should rewrite the code so that the enemy doesn't rely entirely on the agent, right? Because if so I'm down for it, it's just that so far from my tests this was the best way to get my enemies to move from point A to point B then C etc. Thank you for the reply btw.

    There's no need to re-write the agent code at all in the case of relying on physics for the knockback no. What I would do is set the agent location to be itself in order to stop the agent's movement, then disable the agent node entirely while you want the physics and then have the physics code execute after that. Even if the agent gets moved, especially with Godot 4, if it bounces quite far off you should be able to re-enable the agent and then have it continue on a path if that's what you want, hope that makes sense.

    This is purely pseudo code but as an example.

    if agentIsTouchingPlayer == true:
    agent.set_target_position(agent.global_transform.origin) // or just global_transform.orgin depending on where you have your movement code.
    
    // physics stuff afterwards

      Lethn
      Ahhh, I see. That makes sense, I'll try it this way. It didn't even occur to me to do it like this, I'll report back once I have some results. Thank you.