0 votes

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.

Godot version 3.4.2
in Engine by (21 points)
edited by

Please use global-position instead of position

But even if I change to:

velocity = global_position.direction_to(player.global_position) * chase_speed

using direction_to, the result is the same.

2 Answers

0 votes
Best answer

So after a long time fuming my brain cells over this, I found out how silly it was of a bug, created by myself and my lack of experience on the editor.

What happened was, even though my player sprite was in the middle of the screen, it's position in the editor was at the top left corner, and everything I put in the scene that should look for the player was actually looking in that direction.

Now all is well, here is a picture to better illustrate to where it was pointing out, if anybody has the same problem, just make sure to also check this out.
It was alongside my teste node pointer, where it says TesteMundo.

https://imgur.com/a/7DbWFs9

Thanks for everyone who tried helping.

by (21 points)
0 votes

Use direction_to to calculate the direction

by (14 points)

Even if I change to

velocity = position.direction_to(player.position) * chase_speed

It has the same output, the enemy keeps moving to the left*.

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 Frequently asked questions and 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 [email protected] with your username.