0 votes

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

1 Answer

+1 vote

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!

by (399 points)
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.