Random bullet spread

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

var velocity = Vector2(1, 0)
var speed = 1000

var look_once = true

func _process(delta):
	if look_once:
		var mouse_pos = get_global_mouse_position()
		look_once = false
	global_position += velocity.rotated(rotation) * speed * delta

func _on_VisibilityNotifier2D_screen_exited():

Following tutorial, bullets currently spawn fine but I cannot figure out how to get them to deviate slightly by a few degrees when fired, instead of all going exactly where the mouse pointer is.


:bust_in_silhouette: Reply From: Zylann

If you dont want them to go where the mouse is firing, then… where do you want them to deviate to?

If you want random deviation, it can be done by adding a small random angle to the bullet when you initialize its orientation:

if look_once:
    var mouse_pos = get_global_mouse_position()
    var deviation_angle = PI * 0.1
    rotation += rand_range(-deviation_angle, deviation_angle)
    look_once = false

An alternative is to apply randomness to the mouse position, however that would make bullets more “precise” the further away you aim, so you also have to modulate that randomness based on distance which is a bit more complicated.

Looking at your example video, I’m not sure if it’s what you currently have, or if it’s what you want?