0 votes

I'm trying to make this work from few hours. The code is right there are no errors when I start the game its 2d game.the enemy zombie is kinematic

func _process(delta):
var speed = 30
var zombiPos = get_global_position()
var heroPos = hero.get_global_position()
var direction = (get_global_position() - hero.get_global_position()).normalized()
move_and_collide(direction * speed * delta)
Godot version 3.4.2
in Engine by (24 points)
 hero.get_global_position() - get_global_position()

2 Answers

0 votes

The trick with minusing coordinates is to always do target position minus character position. That's to say, if you want the vector starting at vector A and going to vector B then it's B-A

If you imagine vector as arrows, then it's the tip of the arrow minus the nock of the arrow.

So it's:

var direction = (hero.get_global_position()-get_global_position()).normalized()
by (2,087 points)
0 votes

To find a vector pointing from A to B use B - A.

Like following:

var direction = (hero.get_global_position() - get_global_position()).normalized()

You can take a look at Godot Doc.

by (526 points)

Hehe, we posted almost identical answers within seconds of each other. I was first! ;)

Yes, what a coincidence! :)

thanks guys ;) now it's working fine. I thought it wouldn't matter if I write A - B seems like I must learn vector math

Welcome. It helps if you think about standard numbers as 1d vectors (on a number line like in primary school). 5-7 is not the same as 7-5. In 2d you can grab a pen and paper and add two vectors by drawing them head to tail on a graph. A+B and B+A hit the same endpoint but take a different route. If you think about it, that's true in 1d (and 3d) as well. Take a few minutes to do this and you'll see intuitively what it means to add and minus vectors.

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.