Why bullet have wrong velocity?

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

Hello, I’m a new guy in programming. I don’t know why bullet have a wrong velocity when it spawn. I have a few functions _on_ShootCooldown_timeout() and func _on_ExitingTimer_timeout(): First function must spawn bullet. And Second must delete it.
This is code for thats functions:

func _on_ShootCooldown_timeout():
	bullet.initiateTarget(player)
	bullet.position = $Weapon.global_position
	scene.add_child(bullet)
	bullet.look_at(player.global_position)
	$ExitingTimer.start()


func _on_ExitingTimer_timeout():
	scene.remove_child(bullet)

And for bullet we have this code

func _physics_process(delta):
	position += velocity * bullet_speed * delta
   
func makeVelocity():
	velocity = position.direction_to(target.global_position).normalized()
		
func initiateTarget(targ):
	target = targ

Please, anyone help me. I don’t understand when I do mistake.

What class are your bullets, e.g. KineticBody’s?

SteveSmith | 2022-11-23 15:04

Yes, what better class for it?

Nizil | 2022-11-23 15:29

When you say the velocity is wrong what do you mean, is it going in the wrong direction and if so is it always off by the same amount every time?

Gluon | 2022-11-23 15:59

That’s fine, but you need to use move_and_slide rather than change its position directly. And what is the parent of the bullet? You are using its position which may not be its global position.

SteveSmith | 2022-11-23 16:05

no I tryed to use move_and_slide() too, and it didn’t working too. And I make parent for bullet is current scene, where exiting mob

Nizil | 2022-11-23 19:43

Sometimes it makes normal velocity to player, but on next shoot the bullet seems to have a different target. But the function initiateTarget() gets the position of the target correctly

Nizil | 2022-11-23 19:51

And, I’m sorry. I didn’t write this but I use makeVelocity() function in _ready(). I suppose it can be important

Nizil | 2022-11-23 19:58