My Bullets Stop When I Change Direction

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

I’m making a 2d platformer where the character you play as has a gun.
But whenever I shoot to the left, it goes really slowly.
When I shoot to the right it’s completely fine.

Does anyone know how to fix this?

Hi! you really need to share your code so we can help! Its hard to know what is happening if we cannot see the relevant code.

p7f | 2020-09-10 12:26

Sorry about that

Here is the bullet’s code ///

`extends KinematicBody2D

var speed = 1000
var dir
var pos
var velocity = Vector2()
var accel
var loop = 1
var shooty

func _ready():
dir = Pewww.shooty
pos = self.position.x
self.position = Pewww.pos
shooty = Pewww.shooty

func _process(delta):
velocity.x += speed * delta
velocity.y += Pewww.change.y
velocity.x = velocity.x * shooty
move_and_slide(velocity)

if loop == 1:
	loop = 2
	yield(get_tree().create_timer(1.0),"timeout")
	queue_free()

And here is Pewww, the thing that the Bullet’s code takes some stuff from

extends Node

var direction = 1
var shooty = 1
var pos = Vector2.ZERO
var change = Vector2()

func _process(delta):
if Input.is_action_just_pressed(“ui_left”):
shooty = -shooty
if Input.is_action_just_pressed(“ui_right”):
shooty = abs(shooty)

puffle69945 | 2020-09-10 12:33

:bust_in_silhouette: Reply From: Thomas Karcher

With velocity.x += speed * delta you add a positive value to your velocity vector. So the velocity increases when shooting to the right as expected, but decreases (by adding a positive number to a negative velocity) when shooting to the left. So the value is already wrong when you try to fix the direction with velocity.x = velocity.x * shooty. Removing these two lines and writing velocity.x += (speed * delta) * shooty instead should fix the issue.

That fixed it! Thank you very much!

puffle69945 | 2020-09-11 10:16