![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Scavex |
Hello guys. First of all I feel very sad that GODOT messed my project .Everything was fine until I deleted on of my scenes intentionally and the next time I started Godot It gave an error that dependencies not found and due to that I had to again make my Stage 1. coming to the question, prior to the above incident my player was actually able to fire bullets properly (almost since the Position2D wasn’t working when I was placing it in front of my player’s collision shape so I placed it on my player’s head) but now the bullets are not getting fired and when my player falls only then I see a few bullets coming out from my player.
This is my Player Code
extends KinematicBody2D
const UP=Vector2(0,-1)
const GRAVITY=20
const ACCELERATION=50
const MAX_SPEED=150
const JUMP_HEIGHT=-400
const BULLET=preload("res://Bullet.tscn")
var motion =Vector2()
var playerhp=10
var is_dead=false
func _physics_process(delta):
if is_dead==false:
motion.y+=GRAVITY
var friction=false
if Input.is_action_pressed("ui_right"):
motion.x=min(motion.x+ACCELERATION,MAX_SPEED)
$Sprite.flip_h=false
$Sprite.play("Run")
if sign($Position2D.position.x)==-1:
$Position2D.position.x*=-1
elif Input.is_action_pressed("ui_left"):
motion.x=max(motion.x-ACCELERATION,-MAX_SPEED)
$Sprite.flip_h=true
$Sprite.play("Run")
if sign($Position2D.position.x)==1:
$Position2D.position.x=-1
else:
motion.x=0
$Sprite.play("Idle")
friction=true
motion.x=lerp(motion.x,0,0.2)
if is_on_floor():
print("On floor,")
if Input.is_action_just_pressed("ui_up"):
motion.y=JUMP_HEIGHT
if friction == true:
motion.x=lerp(motion.x,0,0.2)
else:
$Sprite.play("Jump")
if friction == true:
motion.x=lerp(motion.x,0,0.5)
if Input.is_action_just_pressed("ui_focus_next"):
var bullet=BULLET.instance()
if sign($Position2D.position.x)==1:
bullet.set_bullet_direction(1)
else:
bullet.set_bullet_direction(-1)
get_parent().add_child(bullet)
bullet.position=$Position2D.global_position
motion=move_and_slide(motion,UP)
if get_slide_count()>0:
for i in range(get_slide_count()):
if "Enemy" in get_slide_collision(i).collider.name:
dead()
func dead():
playerhp-=1
if playerhp<=0:
is_dead==true
motion=Vector2(0,0)
$Sprite.play("Dead")
$CollisionShape2D.disabled=true
$Timer.start()
func _on_Timer_timeout() -> void:
get_tree().change_scene("StartMenu.tscn")
func _on_VisibilityNotifier2D_screen_exited() -> void:
get_tree().reload_current_scene()
This is my code for Bullet
extends Area2D
const SPEED =300
var velocity = Vector2()
var direction=1
func _ready():
pass
func set_bullet_direction(dir):
direction = dir
if dir==-1:
$Sprite.flip_h=true
func _physics_process(delta):
velocity.x=SPEED*delta*direction
translate(velocity)
func _on_VisibilityNotifier2D_screen_exited() -> void:
queue_free()
func _on_Bullet_body_entered(body: Node) -> void:
if "Enemy" in body.name:
body.dead()
queue_free()
So I want to ask that it was almost working before but why isn’t it working now. One thing I also want to mention is that I created a separate scene for my PLAYER and added it as an instance to my Stage1 Scene. Please help me out since it’s frustrating me.