+1 vote

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)
in Engine by (22 points)

1 Answer

+2 votes
Best answer

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.

by (1,544 points)
selected by

Yes thank you, it works perfectly

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

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.