Bullets are getting slower when my mouse get close to my character and quicker the farther it is

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

Hello, i am new to godot and i’m having some trouble making the bullets shoot at a certain speed. when my mouse is near my character the bullets will move slowly and if it’s farther it will go too fast. Can someone help me out ?

thank you.

this is my player scene

extends StaticBody2D

export var BulletScene : PackedScene

onready var arrowSprite = $Position2D/ArrowSprite
onready var arrow2D = $Position2D

var mousePosition

func _ready():
	mousePosition = get_global_mouse_position()

func _process(delta):
	if Input.is_action_pressed("shoot"):
		mousePosition = get_local_mouse_position()

func shoot():
	var bulletInstance = BulletScene.instance()
	var main = get_tree().current_scene
	bulletInstance.global_position = arrowSprite.global_position

My bullet scene

extends Area2D

var bulletSpeed = 10
var dir

func _process(delta):
	position += dir * bulletSpeed * delta

func shootDirection(mouseDirection):
	dir = mouseDirection

func _on_VisibilityNotifier2D_screen_exited():
:bust_in_silhouette: Reply From: RedBlueCarrots

This will be because the mouseDirection argument you are passing is not normalized.

if you change the bullet script to be dir = mouseDirection.normalized(), this will create a unit vector.

At the moment, if the bullet is 200 units away from the mouse, on the x axis, dir will equal (200, 0), whereas if it is 100 units away, dir will equal (100, 0). (thus half the speed)

A unit vector means that the vector will return a vector with speed = 1 in the direction of the vector. so both (200, 0) and (100, 0) will return (1, 0), and a vector (30, 40) will return (0.6, 0.8)

Hope that helps!