0 votes

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.

Godot version latest
in Engine by (34 points)

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

Yes, what better class for it?

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?

That's fine, but you need to use moveandslide 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.

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

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

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

Please log in or register to answer this question.

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.