How to change direction of the bullets?

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

Hi everyone! Today I have a problem that I can’t solve: I have a 2D player that shoots projectiles in one direction. However, when I change the horizontal direction of the player (with set flip h (true)), the bullets continue in the same direction. How can I solve it? Thank you!

Hi Rob ! There are many different ways to make projectiles. Can you tell us how you implemented the movement of the bullet ? It would be easier to help you then

nonomiyo | 2019-01-13 09:11

Hi, idk if i understood, but the problem is that when you flip the character, the bullet change its direction to where the character is looking? If so, it’s probably because you added bullet as child of character instead of as child of root node

p7f | 2019-01-13 14:10

Hi! This is the projectile script:

extends Area2D
var vel = Vector2()
export var speed = 1000

func _ready():
set_physics_process(true)

func start_at(dir, pos):
set_rotation(dir)
set_position(pos)
vel = Vector2(speed, 0).rotated(dir)

func _physics_process(delta):
set_position(get_position() + vel * delta)

Rob1980 | 2019-01-14 01:37

Hi, just for the record, try to format your code like this:

extends Area2D

var vel = Vector2()
export var speed = 1000

func ready():
    set_physics_process(true)

func startat(dir, pos):
    set_rotation(dir)
    set_position(pos)
    vel = Vector2(speed, 0).rotated(dir)

func _physics_process(delta):
    set_position(getposition() + vel * delta)

You must indent with 4 spaces so the code formats correclty.

Anyways, in first place, i must ask if my assumption to what the problem was is correct. And if it so, what we need to help you is not the bullet script, but the script where you instance it. I mean, the script where the bullet is created and everything.

p7f | 2019-01-14 01:43

Hi! My problem is that, unfortunately, when I flip the character horizontally, the bullet doesn’t flip.

The bullet is created in the character script, this:

extends Area2D
export (int) var SPEED 
var velocity = Vector2()
var screensize
onready var bullet = preload("res://bullet.tscn")
onready var bullet_container = get_node("bullet_container")
onready var gun_timer = get_node("gun_timer")

export var max_health = 10
var health = max_health

func _ready():
 print(get_owner())
 screensize = get_viewport_rect().size
 set_process(true)
onready var node = get_node("/root/bullet")
 
func _process(delta):
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.set_flip_h(false)
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		$AnimatedSprite.set_flip_h(true)
		velocity.x -= 1 
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1.5
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1.5
	if Input.is_action_pressed("ui_select"):
		if gun_timer.get_time_left() == 0:
			$AnimatedSprite.set_animation("Cammy2")
			$AudioStreamPlayer.play()
			shoot()
	if velocity.length() > 0:
		velocity = velocity * SPEED
		
	position += velocity * delta
	position.y = clamp(position.y, 50, 560)
	position.x = clamp(position.x, 50, 1000)
	
func start(pos):
	position = pos
	show()
	monitoring = true
	
func shoot():
	gun_timer.start()
	var b = bullet.instance()
	bullet_container.add_child(b)
	b.start_at(get_rotation(), get_position())

func _on_gun_timer_timeout():
	$AnimatedSprite.set_animation("Cammy")

Rob1980 | 2019-01-14 01:52

And what is the structure of your bullet? i know that it extends Area2D… but then it has an Sprite as child?

p7f | 2019-01-14 01:59

Yes, yes, this is the structure:

  • Area2D
    –Sprite
    –CollisionShape2D

Rob1980 | 2019-01-14 02:27

:bust_in_silhouette: Reply From: LordViperion

Try the vector2.reflect(normal) method or If you want change only horizontal dir then multiply, vector2.x *= -1

Thanks, but my problem is not to know how to flip the direction of the projectile; in fact, I could also use the variable vel = Vector2 (speed, 0).rotated (dir + PI).
My problem is that I don’t know how to implement this command correctly in an “if”.

This is the projectile script:

extends Area2D

var vel = Vector2()
export var speed = 1000

func _ready():
	set_physics_process(true)
	
func start_at(dir, pos):
	set_rotation(dir)
	set_position(pos)
	vel = Vector2(speed, 0).rotated(dir)
	
	
func _physics_process(delta):
	set_position(get_position() + vel * delta)

And this is the character’s script:

extends Area2D

export (int) var SPEED 
var velocity = Vector2()
var screensize
onready var bullet = preload("res://bullet.tscn")
onready var bullet_container = get_node("bullet_container")
onready var gun_timer = get_node("gun_timer")

export var max_health = 10
var health = max_health

func _ready():
 print(get_owner())
 screensize = get_viewport_rect().size
 set_process(true)
onready var node = get_node("/root/bullet")
 
func _process(delta):
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		$AnimatedSprite.set_flip_h(false)
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		$AnimatedSprite.set_flip_h(true)
		velocity.x -= 1 
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1.5
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1.5
	if Input.is_action_pressed("ui_select"):
		if gun_timer.get_time_left() == 0:
			$AnimatedSprite.set_animation("Cammy2")
			$AudioStreamPlayer.play()
			shoot()
	if velocity.length() > 0:
		velocity = velocity * SPEED
		
	position += velocity * delta
	position.y = clamp(position.y, 50, 560)
	position.x = clamp(position.x, 50, 1000)
	
func start(pos):
	position = pos
	show()
	monitoring = true
	
func shoot():
	gun_timer.start()
	var b = bullet.instance()
	bullet_container.add_child(b)
	b.start_at(get_rotation(), get_position())

func _on_gun_timer_timeout():
	$AnimatedSprite.set_animation("Cammy")

Rob1980 | 2019-01-14 01:54

:bust_in_silhouette: Reply From: p7f

Maybe the problem is that you are setting bullet’s rotation equal to your player’s rotation with this:

b.start_at(get_rotation(), get_position())

But i don’t see you changed the rotation of the player, only flipped the sprite. So, your player rotation is still 0, am i right? What you can do is to add PI to the rotation if the $AnimatedSprite.flip_h is true. Something like this:

func shoot():
    gun_timer.start()
    var b = bullet.instance()
    var rotation = get_rotation()
    if $AnimatedSprite.flip_h:
        rotation += PI
    bullet_container.add_child(b)
    b.start_at(rotation, get_position())

Please, try it and tell me if it works.

Also, if the player is not going to rotate in other angles, you coulds just use PI as rotation if flip_h is true, or 0 else case.

p7f | 2019-01-14 13:06

IT WORKS! Thank you so much! :wink:

Rob1980 | 2019-01-15 05:21

You are welcome!

p7f | 2019-01-15 09:49

:bust_in_silhouette: Reply From: pattyorigami

Assuming you’re only concerned with horizontal movement, the simplest solution would be to have the player script launch a function in the bullet’s script when the player instances it, and have the player’s code plug in a direction variable as an argument in that function in the bullet’s code:

So the bullet’s script can have a variable called direction
var dir = 1

in the bullet’s physics process, just set it to automatically travel upon being created:

func _physics_process(delta: float) → void:
motion.x = Speed * delta * dir
translate(motion)

translate( ) or move_and_slide or move_and_collide, depending on what kind of node your bullet is. Mine is an Area2D, so I used translate(motion)

With that direction variable, you’ll be able to use it to tell the bullet whether to flip_h or not:

if dir == -1:
$Sprite.flip_h = true

and if direction is a -1 it will make it travel left, but we’ll need the player who creates the bullet to tell our bullet’s script which value direction will be. So let’s create a function here in the bullet script that the player will access when he creates a bullet.
Maybe something like:

set_bullet_direction(d):
dir = d

that little “d” inside the parentheses is like a little teleporter that can receive variables from other scripts that call this function. I called it “d” but you can call it anything you like. Just know that it is a container that will receive a piece of info from the player.

Now in the player’s script, figure out a way to tell your player’s direction. Could be something like this:

var direction = 1
if Input.is_action_just_pressed(“ui_left”):
direction = -1
elif Input.is_action_just_pressed(“ui_right”):
direction = 1

Or you could do something similar with motion.x being greater than or less than 0 to set the direction variable. However you want to set direction to either 1 or -1, based on your player’s movement
Now we create a variable to represent the bullet that we instance. I called mine bullet whereas the preloaded bullet was a constant, so I called that BULLET

const BULLET = preload(“res://Objects/Bullet.tscn”)
var bullet = BULLET.instance( )

then, before we add the child to the scene, we’ll use our bullet variable to access the function in the bullet’s script and plug in our own direction variable:

bullet.set_bullet_direction(direction)

notice how I put the player’s variable “direction” inside the parentheses where in the player script we have a “d”? Basically that makes the bullet’s script plug in “direction” in place of that “d” and sets that value of either 1 or -1!

And then, finally we spawn the bullet:

get_parent().add_child(Bullet)

After that you can set the bullet’s position, but I’ll not get into that now, assuming you already know how to do that. Hope this was helpful.

1 Like

OMG THANK YOU SO MUCH. You’re a lifesaver. I’m not the OP and I’m not sure if you’re gonna see this but, again, you definitely saved me hours of headaches. I just created an account just saying this and thank you for making this as detailed as possible.