This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

extends KinematicBody2D

var speed = 500
var velocity = Vector2()
var bullet_speed = 2000
var bullet = preload("res://Bullet.tscn")

func getinput():
velocity = Vector2()
if Input.is
actionpressed("right"):
velocity.x += 1
if Input.is
actionpressed("left"):
velocity.x -= 1
if Input.is
actionpressed("down"):
velocity.y += 1
if Input.is
action_pressed("up"):
velocity.y -= 1
velocity = velocity.normalized() * speed

look_at(get_global_mouse_position())

func physicsprocess(delta):
getinput()
velocity = move
and_slide(velocity)

if Input.is_action_pressed("LMB"):
    fire()

func fire():
var bulletinstance = bullet.instance()
bullet
instance.position = getglobalmouseposition()
bullet
instance.rotatedegress = rotationdegrees
bulletinstance.applyimpulse(Vector2(),Vector2(bulletspeed,0).rotated(rotation))
get
tree().getroot().calldeferred("addchild",bulletinstance)

Godot version 3.5.1
in Engine by (12 points)

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().

no, I tried didn't work still freezing I have tried every and it just does not work. thank you for trying to help

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...

1 Answer

0 votes

inside your fire function change this

gettree().getroot().calldeferred("addchild",bulletinstance)

to this

get_parent.add_child(bulletinstance)

maybe solve the problem

by (286 points)

no still not working
debugger: Parser error: the identifier "bulletinstance" isn't declared in the current scope

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.