+1 vote

So, I have two KinematicBody2D nodes, the Player, and the enemy. I want the enemy to be able to move towards the player, but with the way that I'm doing it (move_toward()), the enemy completely ignores the StaticBody2D nodes (walls / ground) and just goes right through them. My code is below (Sorry about indents, I'm new to the Q&A and don't know how to correctly do them):
extends KinematicBody2D

var motion = Vector2()
var player

func _enter_tree(): player = get_node("/root/Level1/Player")

func _physics_process(_delta):
look_at(player.position)
position.x += move_toward(position.x, player.position.x, player.speed)
position.y += move_toward(position.y, player.position.y, player.speed)

in Engine by (13 points)

2 Answers

0 votes

Maybe the move toward function ignores physics? (not sure). Try the kinematic move and slide function.

be aware that the enemy won't navigate around the collisions, he will just hopefully not go through them.

hope it helps. :)

by (365 points)
0 votes

If you want to use physics (collisions, etc) you cannot just set the position.
You have to use smothing like moveandslide or moveandcollide.

As a very very very dirty fix, i think it would work if you add move_and_slide(Vector2.ZERO) at the end. (untested!)

by (1,018 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 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 webmaster@godotengine.org with your username.

Categories