Hi, I'm stuck on this problem, since I can't really see what I'm doing wrong.
I hope it's not a silly thing, here is the code:
extends KinematicBody2D
var velocity = Vector2.ZERO
var chase_speed = 100
var player = null
func _ready():
add_to_group("inimigos")
func _physics_process(delta):
if player:
velocity = (player.position - position).normalized() * chase_speed
move_and_slide(velocity)
func _on_DetectandoPlayerArea_body_entered(body):
if body.is_in_group("player"):
player = body
print("Eu vejo vocĂȘ")
func _on_DetectandoPlayerArea_body_exited(_body):
player = null
I really hope this hasn't been answered before, I'm searching for this for hours, and sometimes we just can't find the right words to search for.
The detection works as planned, pretty simple, but somehow to enemy just keeps drifting away from the player towards the LEFT always.
If I make velocity negative, it drifts towards the RIGHT instead, but never follows the player.
Edit: So I printed the values like this:
print("Velocity: ",velocity,"\n","PosiPlayer: ", player.global_position, "\n", "PosiEnemy: ", global_position)
The results:
Velocity: (-97.145355, -23.722973)
PosiPlayer: (375.452332, -109.84481)
PosiEnemy: (1157.895752, 81.2285)
So again, just chaging velocity by multiplying it to -1 or itself to -, simply changes the position it slides instead of left, it slides right.
The Player Node is on Layer 1, Mask 1.
The enemy is on Layer 3, Mask 1.
Putting them on the same layer does nothing different aswell.
To illustrate here what happens:
https://imgur.com/a/2VN7nTF
Another new edit here, I started a new clean project on the same engine version, I still get the same bug.