I'm working on a top down shooter, but whenever I try to shoot, it shoots where I placed my bullet in my bullet scene.
Kinematic body with sprite, CollisionShape 2d, and node 2d.
2dNode Firepoints with 8 firepoints in it.
Code:
extends KinematicBody2D
var speed = 200
var velocity = Vector2.ZERO
var bullet = preload("res://Bullet.tscn")
var firePlace = Vector2.ZERO
var fireRate = 0.3
var fireTime = 0.0
onready var bulletSpawn = $FirePoints.get_children()
func physicsInput():
velocity = Vector2.ZERO
if Input.is_action_pressed('moveRight'):
velocity.x += 1
firePlace.x = 1
if Input.is_action_pressed('moveLeft'):
velocity.x -= 1
firePlace.x = -1
if Input.is_action_pressed('moveDown'):
velocity.y += 1
firePlace.y = 1
if Input.is_action_pressed('moveUp'):
velocity.y -= 1
firePlace. y = -1
velocity = velocity.normalized() * speed
func regInput():
if Input.is_action_just_pressed("attack"):
shoot()
if Input.is_action_just_pressed("exit"):
get_tree().quit()
if Input.is_action_just_pressed("restart"):
get_tree().reload_current_scene()
func _physics_process(delta):
physicsInput()
velocity = move_and_slide(velocity)
func _process(delta):
regInput()
func shoot():
firePoint()
if get_time() - fireTime < fireRate:
return
fireTime = get_time()
var bul = bullet.instance()
get_tree().get_root().add_child(bul)
func firePoint():
var bul = bullet.instance()
if firePlace == Vector2(1,1):
bul.global_position = $FirePoints/FirePoint4.global_position
if firePlace == Vector2(1,0):
bul.global_position = $FirePoints/FirePoint3.global_position
if firePlace == Vector2(1,-1):
bul.global_position = $FirePoints/FirePoint2.global_position
if firePlace == Vector2(0,1):
bul.global_position = $FirePoints/FirePoint5.global_position
if firePlace == Vector2(-1,1):
bul.global_position = $FirePoints/FirePoint6.global_position
if firePlace == Vector2(1,0):
bul.global_position = $FirePoints/FirePoint7.global_position
if firePlace == Vector2(-1,1):
bul.global_position = $FirePoints/FirePoint8.global_position
else:
bul.global_position = $FirePoints/FirePoint1.global_position
func get_time():
return OS.get_ticks_msec() / 1000.0