How to make my bullet speed the same, no matter what the vector is

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

So I have a KinematicBody2D acting as my bullet, and it shoots in the direction of my mouse. However, I noticed that if my mouse position is farther away from my gun position, the bullet shoots much faster, and if it is closer to my gun position, the bullet shoots much slower. I was wondering if I could somehow make it so that the bullet’s speed is the same despite the vector. Here is my code:

Weapon code:

func shoot():
var mouse_pos = get_global_mouse_pos()
var pos = get_global_pos()
var direction = mouse_pos - pos
direction.normalized()
bullet.shoot(direction) # pass rotation to bullet

bullet code:

func shoot(direction):
    var _direction = direction
    set_fixed_process(true)

func _fixed_process(delta):
    var vel = _direction*bullet_speed*delta
    move(vel)
:bust_in_silhouette: Reply From: Diet Estus

normalized() returns a new vector. It does not normalize the old vector. See the Vector2 documentation.

So you need to change

direction.normalized()

into

direction = direction.normalized()

That should get your code working.

Yes thank you, it works perfectly

CosmicNerd6505 | 2018-04-19 01:31

Great! You can click the check to select it as the answer. It may help others in the future.

Diet Estus | 2018-04-19 01:33