- Edited
Hi there I'm currently trying to learn Godot through remaking arcade/8&16-bit/classic era game mechanics, with much trial and error. The project I'm currently trying to make work is the shooting mechanics of Wild Guns (
) but I can't seem to get my bullet to fire towards my crosshair, it only fires to the right of its own spawn position. I assume the best way is to use a Raycast2d, which I'm not using currently, but even then I'm not sure how to set it's trajectory from the bullets spawn position to the crosshair centre. The crosshair, along with the player character, is controlled with WASD and is a child of the player and both have their own movement scripts. The bullet spawns from a Position2D which is named "Gun" and the crosshair has a centre point Position2D named "CrosshairCentre", I've tried a few things that I thought may change the bullet trajectory from the spawn but it either resulted in the bullets not spawning, firing to the right like before or just plain crashing.
Player script
extends KinematicBody2D
export (int) var speed = 200
var velocity = Vector2()
onready var bullet = preload("res://Bulit.tscn")
var b
onready var target = $CrossHair/CrosshairCentre
func shoot():
if Input.is_action_just_pressed("fire"):
b = bullet.instance()
get_parent().add_child(b)
b.global_position = $Gun.global_position
func move():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
velocity.x += 1
if Input.is_action_pressed("ui_left"):
velocity.x -= 1
if Input.is_action_pressed("fire"):
speed = 0
if Input.is_action_just_released("fire"):
speed = 200
velocity = velocity.normalized() * speed
func _physics_process(_delta):
shoot()
move()
velocity = move_and_slide(velocity)
bullet script
extends RigidBody2D
var b_speed = 500
func _ready():
apply_impulse(Vector2(), Vector2(b_speed, 0).rotated(rotation))
scale = Vector2(.2, .2)
If anyone can help me or at least point me to somewhere that can I would very much appreciate it
EDIT: Fixed the formatting and updated some information.