Global mouse position return vector zero

Godot Version

3.5.3

Question

My goal is to making bullets spawn spreadly in cone. However, the bullet are not changing direction when the player rotates. I tried to add the mouse position to the bullet vector but it keeps return vector zero with a error of get_viewport. Here my player code, I hope it can help:extends Node2D
onready var shoot_position = $Muzzle
signal shooting
var speed = 400
var bullet = preload(“res://Bullet.tscn”)
var b = bullet.instance()
var veloc = get_global_mouse_position()
func _ready():
add_to_group(“player”)
get_viewport()
func _process(delta):
var dir = Vector2.ZERO
if Input.is_action_pressed(“ui_right”):
dir.x += 1
if Input.is_action_pressed(“ui_left”):
dir.x += -1
if Input.is_action_pressed(“ui_up”):
dir.y += -1
if Input.is_action_pressed(“ui_down”):
dir.y += 1
position += speed * dir * delta
look_at(get_global_mouse_position())
if Input.is_action_just_pressed(“shoot”):
shoot()
print(veloc)
func shoot():
var b = bullet.instance()
emit_signal(“shooting”)
get_parent().add_child(b)
b.global_position = shoot_position.global_position
b.direc = Vector2(rand_range(15,20), rand_range(-6, 6)) + veloc

Hi tuvudang07
I’m fairly new to GDscript, but have you tried adding the mouse position as separate X/Y positions?
b.direc = Vector2(rand_range(15,20) + veloc.x, rand_range(-6, 6) + veloc.y)