0 votes

Hi, i have node called "ghost" which should follow the player everywhere, however, it does not.
It just keeps going to the right, even thought it counts well the angle. I suspect there is something broken in the calculation of the final move vector, but since it is simple trigonometry, I believe I setted it up correctly.
I would gladly accept any tips on what might be wrong.
How does it look like: (the godot icon should follow the player)
enter image description here

Here is my ghost code:

extends KinematicBody2D
var reached = false
const SPEED = 1
const ROTATION_SPEED = 20

func _ready():
    set_process(true)
    pass

func _process(delta):
    var ang = get_angle_to(get_node("../player").get_pos())
    rotate(ang*ROTATION_SPEED*delta)
    var x = cos(ang)
    var y = -sin(ang)

if !reached:
    move(Vector2(x*SPEED,y*SPEED))


func _on_player_body_enter( body ):
    reached = true

func _on_player_body_exit( body ):
    reached = false

the final vector of the ghost (/ kinematicbody2D) is always 1 on x. how so?

in Engine by (17 points)

1 Answer

+1 vote
Best answer

I see an error there:

func _process(delta):
    var ang = get_angle_to(get_node("../player").get_pos())
    rotate(ang*ROTATION_SPEED*delta)
    var x = cos(ang)
    var y = -sin(ang)

if !reached:
    move(Vector2(x*SPEED,y*SPEED))

The final if is not indented to the func _process, so is not executed every time the process function is called. By the way, an easier way to get the direction the ghost must go is just substracting the players position to the ghosts position, this will give you the ghost directional vector, just remember to normalize it.

var direction =  (get_node("../player").get_pos() - get_pos()).normalized()
by (697 points)
selected by
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.