![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | A godot user |
extends KinematicBody2D
var speed = 500
var velocity = Vector2()
var bullet_speed = 2000
var bullet = preload(“res://Bullet.tscn”)
func get_input():
velocity = Vector2()
if Input.is_action_pressed(“right”):
velocity.x += 1
if Input.is_action_pressed(“left”):
velocity.x -= 1
if Input.is_action_pressed(“down”):
velocity.y += 1
if Input.is_action_pressed(“up”):
velocity.y -= 1
velocity = velocity.normalized() * speed
look_at(get_global_mouse_position())
func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity)
if Input.is_action_pressed("LMB"):
fire()
func fire():
var bullet_instance = bullet.instance()
bullet_instance.position = get_global_mouse_position()
bullet_instance.rotate_degress = rotation_degrees
bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed,0).rotated(rotation))
get_tree().get_root().call_deferred(“add_child”,bullet_instance)
Just a guess, but it looks like you may be spamming the fire()
function call since you’re using is_action_pressed()
. That’ll call the function in every physics frame where the button is held down (so, by default, 60x per second). Assuming you want to fire only once per button press, change that call to is_action_just_pressed()
.
jgodfrey | 2022-12-12 23:53
no, I tried didn’t work still freezing I have tried every and it just does not work. thank you for trying to help
A godot user | 2022-12-13 00:59
Hmmm… Unformatted code is really difficult to read, but I notice that you might have a typo here:
bullet_instance.rotate_degress = rotation_degrees
That rotate_degress
can’t be right, can it?
Further, assuming the problem is in the fire()
function, you can try to comment out individual lines to find the offending code and further narrow down the problem…
jgodfrey | 2022-12-13 01:25