+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)

asked May 27 in Engine by CupOfTea134 (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. :)

answered May 28 by Millard (141 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!)

answered May 28 by whiteshampoo (987 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.