Attention | Topic was automatically imported from the old Question2Answer platform. | |
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