- Edited
I am trying to make a projectile that shoots when the player clicks the left mouse button. It is not working. when the player clicks the shoot button, the projectile just sits there. Here is my code:
Player:
extends KinematicBody2D
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
var screen_size
export (int) var speed = 1000
export(PackedScene) var bullet
var velocity = Vector2()
# Called when the node enters the scene tree for the first time.
func _ready():
screen_size = get_viewport_rect().size
func start(pos):
position = pos
# Change the target whenever a touch event happens.
func get_input():
look_at(get_global_mouse_position())
velocity = Vector2()
if Input.is_mouse_button_pressed(BUTTON_LEFT):
shoot()
if Input.is_action_pressed("ui_select"):
velocity = Vector2(speed, 0).rotated(rotation)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(_delta):
get_input()
velocity = move_and_slide(velocity)
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
func shoot():
var b = bullet.instance()
self.add_child(b)
b.transform = $Muzzle.transform
Bullet:
extends Area2D
# Declare member variables here. Examples:
export var speed = 750
# Called every frame. 'delta' is the elapsed time since the previous frame.
func physics_process(delta):
position += transform.x * speed * delta
#this is to make the bullet fly to the right when fired
Any help is appreciated. Thanks!