Problem involving vectors and shooting at the player

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Carlos Pazuzu

I’m having a problem with vector math as I want to make enemies shoot on current player position. I’m aware of the required vector difference operation, when I do this:

var direction = enemy.position - player.position

the game works like that:
works but not the way i need

and it shoots perfectly at the player, but it is based on distance, so the closer, the slower

of course, I need it to be distance-independent, so I do the proper vector math afterwards:

direction = direction.normalized()
direction *= scale

but it end up shooting next to the player, but not in the precise place, like this:

enter image description here

I’ve tried everything. Nothings makes it go to the right place after normalize the vector.
Can someone help me??

How are you moving your bullets, and what is scale? Ideally, you should find the direction vector (normalized) and then move your bullet in that direction at its speed (i.e. velocity = direction * speed).

kidscancode | 2018-12-30 02:52

THAT was my problem, I was scaling it by a small number like 10, when I tried it with 200 its direction gone to the right place, I feel stupid :stuck_out_tongue: thanks anyway!!!

Carlos Pazuzu | 2018-12-30 02:56

:bust_in_silhouette: Reply From: kidscancode

For future searchers, I’ll add this as an answer:

Find the direction vector (normalized) and then move your bullet in that direction at its speed (i.e. velocity = direction * speed).