bullets aren't accurate

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

HI, ok basically i’m trying to make a gun in godot and it’s not working out too well the bullets are flying everywhere not following the mouse at all except when the player isn’t moving even then they’re too slow and sometimes it goes the complete opposite way
i used an instance of a scene for the bullet here’s my code:

func createBulletInstance() -> void:
	if timeBetweenShots <= 0 && not global.first_bullet:
		var b = bullet.instance()
		add_child(b)
		b.start(global.bullet_spawn_pos, global.dif_bullet_mouse)
		timeBetweenShots = maxTimeBetweenShots
		
	else:
		timeBetweenShots -= get_process_delta_time()

that’s in the main scene script and here’s the bullet script:

    extends Area2D

export (int) var speed
export (int) var damage
var velocity = Vector2()
var difBulletMouse: Vector2

func start(_positon, _direction):
	position = _positon
	rotation_degrees = global.gun_rotation
	velocity = _direction * speed
	print(_direction, speed)

func _process(delta: float) -> void:
#	print(velocity, delta)
	global.dif_bullet_mouse = calculateGlobalDifBulletMouse()
	position += velocity * delta

func calculateGlobalDifBulletMouse() -> Vector2:
	difBulletMouse = get_global_mouse_position() - global_position
	return difBulletMouse

here’s an example of what the console prints ( _direction, speed):

i tried shooting at exactly the same place over and over

(1064.097046, -28.269012)2
(504.196075, 121.085732)2
(-1106.026978, -303.778748)2
(2494.589355, 624.708252)2
(313.460938, 33.704681)2
(-834.855591, -42.236572)2

here’s the print for (velocity, delta):

(0, 0)0.016667
(0, 0)0.016667
(0, 0)0.016667

this is where i shoot

(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667
(1548.19397, 239.461975)0.016667
(0, 0)0.016667

#loops forever

Thanks for trying to help!!!

why are you doing global.dif_bullet_mouse = calculateGlobalDifBulletMouse() in _process ?

supper_raptor | 2020-04-30 14:18

Remove global.dif_bullet_mouse = calculateGlobalDifBulletMouse() in _process from bullet scene

supper_raptor | 2020-04-30 14:21

i put it in the start function it works better but is still a bit weird
some of the bullets are slower and the first one didn’t even fire
also it’s not very precise for example if i move my mouse and click
it will not shoot where my mouse is but a where my mouse was a couple seconds ago
Thanks for your help though :slight_smile:

ROBOTOO007 | 2020-04-30 14:41

:bust_in_silhouette: Reply From: ROBOTOO007

I FIXED IT! thanks to a couple youtube videos and supper_raptor
here’s the new and improved code:

func createBulletInstance() -> void:
	if timeBetweenShots <= 0:
		var b = bullet.instance()
		add_child(b)
		var dir = Vector2(1,0).rotated($gun/gun.global_rotation)
		b.start(global.bullet_spawn_pos, dir)
		timeBetweenShots = maxTimeBetweenShots

#and in the bullet script:

export (int) var speed
export (int) var damage
var velocity = Vector2()
var difBulletMouse: Vector2

func start(_positon, _direction):
	position = _positon
	rotation_degrees = global.gun_rotation
	velocity = _direction * speed
	global.dif_bullet_mouse = calculateGlobalDifBulletMouse()

func _process(delta: float) -> void:
	position += velocity * delta

func calculateGlobalDifBulletMouse() -> Vector2:
	difBulletMouse = get_global_mouse_position() - global_position
	return difBulletMouse