Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My tutorial code to knockback an enemy does not work because of move_toward

I was watching a tutorial and I followed the code the same as in the video, but the tutorial is for godot 3.2 and being on godot 4 it does not work when attacking the enemy and I get an error that says:
Invalid call. Nonexistent function ‘move_toward’ in base ‘bool’.
This is the code:

extends CharacterBody2D

var knockback = Vector2.ZERO

func _physics_process(delta):
    knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)
    knockback = move_and_slide(knockback)

func _on_hurtbox_area_entered(area):
    knockback = Vector2.RIGHT * 200

I tried to delete the move_and_slide arguments and tried to change the move_toward to move_and_slide but it doesn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

What is happening is that move_and_slide returns bool (true if it collider, false if it didn’t).

Thus, here:

knockback = move_and_slide()

The knockback variable becomes a bool. Thus, next frame, when you try to do this:

knockback = knockback.move_toward(Vector2.ZERO, 200 * delta)

Godot can’t call move_toward on a bool, and give you an error.


You would have gotten the error earlier if you make knockback a typed variable, which would be explicitly like this:

var knockback:Vector2 = Vector2.ZERO

Or implicitly (with inference) like this:

var knockback:= Vector2.ZERO

Furthermore, as you know, in Godot 4 move_and_slide no longer takes a velocity, instead you are supposed to set the velocity property. Which means, you could make away with not having a knockback variable.

The code would be like this:

extends CharacterBody2D

func _physics_process(delta):
    velocity = velocity.move_toward(Vector2.ZERO, 200 * delta)
    move_and_slide()

func _on_hurtbox_area_entered(area):
    velocity += Vector2.RIGHT * 200

Here the line using move_toward is performing deceleration.

Presumably you will have other code manipulating velocity, thus double check if you are not doing deceleration already (in which case you do not need to do it here), and if adding deceleration do not mess with anything else.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading