Tomas Daniel
Tomas Daniel

Reputation: 11

Godot area2D bullet, speed isn't the same depending on the direction

I am making a top-down 2D game in Godot 3.1. In where the player shoots a bullet where it's looking at.

I am using Godot 3.1 and Gdscript. I have a bullet scene with the following nodes

Area2D
    > Sprite
    > CollisionShape2D
    > Timer

With this code I make the bullet move

func shoot():
    if canShoot:
        canShoot = false
        var current_rotation = Vector2(1, 0).rotated($".".global_rotation)
        emit_signal('shoot', Bullet, $".".global_position, current_rotation)

func _bulletShoot(Bullet, _position, _direction):
    var bulletInstance = Bullet.instance()
    add_child(bulletInstance)
    bulletInstance.start(_position, _direction)

func start(_position, _direction):
    direction = _direction
    position = _position
    rotation = _position.angle()
    velocity = _position * speed

func _physics_process(delta):
    position += velocity * delta * direction

But the speed is not always the same depending on which direction I shoot. Is there a way to fix it?

I expect the bullet to go to the direction it was fired with a constant speed without varying the direction it was fired.

What is happening is that the bullet goes slower if I point it to 0°, and goes faster if I point it 180°.

Upvotes: 0

Views: 1115

Answers (1)

Elephant
Elephant

Reputation: 456

You should add .normalized()to the direction- this will make it so the component x and y add to 1 but keep the correct ratio. This will mean direction will maintain a constant multiplier to the speed but still give the correct direction

Upvotes: 0

Related Questions