Godot Version
4.x
Question
Hello. Im having a problem with a gun mechanic in my game. Every time i shoot, the bullet is off by a large amount from where it should be. Any help is appreciated.
Code for gun
extends Node2D
var rot_state = 0
@onready var BULLET = preload("res://Scenes/bullet.tscn")
func check_input():
if Input.is_action_pressed("Right") and Input.is_action_pressed("Up"):
rot_state = 1
elif Input.is_action_pressed("Left") and Input.is_action_pressed("Up"):
rot_state = 3
elif Input.is_action_pressed("Right"):
rot_state = 0
elif Input.is_action_pressed("Up"):
rot_state = 2
elif Input.is_action_pressed("Left"):
rot_state = 4
else:
pass
func rotate_node():
if rot_state == 0:
rotation = 0
elif rot_state == 1:
rotation = deg_to_rad(-45)
elif rot_state == 2:
rotation = deg_to_rad(-90)
elif rot_state == 3:
rotation = deg_to_rad(-135)
elif rot_state == 4:
rotation = deg_to_rad(-180)
func shoot():
var b = BULLET.instantiate()
owner.add_child(b)
b.transform = $Marker2D.global_transform
func _physics_process(delta):
check_input()
rotate_node()
if Input.is_action_just_pressed("Shoot"):
shoot()
Code for Bullet
extends Area2D
var speed = 750
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
global_position += transform.x * speed * delta
func _on_body_entered(body):
if body.is_in_group("enemy"):
body.queue_free()
queue_free()