0 votes

THE PLAYER SCRİPT

extends KinematicBody2D

var velocity = Vector2()
const SPEED = 30
const GRAVITY = 7
const FLOOR = Vector2(0, -1)
const JUMP_POWER = -150

const BULLET = preload("res://Bullet.tscn")

var onground = false
var gun
activated = false

func physicsprocess(delta):
if Input.isactionjustpressed("Release-Unrelease"):
if gun
activated == false:
gunactivated = true
elif gun
activated == true:
gun_activated = false

if Input.is_action_pressed("ui_right"):
    velocity.x = SPEED
    if gun_activated == false:
        $AnimatedSprite.play("Run")
    elif gun_activated == true:
        $AnimatedSprite.play("Grun")
    $AnimatedSprite.flip_h = false
    if sign($Position2D.position.x) == -1:
        $Position2D.position.x *= -1
elif Input.is_action_pressed("ui_left"):
    velocity.x = -SPEED
    if gun_activated == false:
        $AnimatedSprite.play("Run")
    elif gun_activated == true:
        $AnimatedSprite.play("Grun")
    $AnimatedSprite.flip_h = true
    if sign($Position2D.position.x) == 1:
        $Position2D.position.x *= -1
else:
    velocity.x = 0
    if on_ground == true:
        if gun_activated == false:
            $AnimatedSprite.play("idle")
        elif gun_activated == true:
            $AnimatedSprite.play("Gunidle")

if Input.is_action_pressed("ui_up") || Input.is_action_pressed("Jump"):
    if on_ground == true:
        velocity.y = JUMP_POWER
        on_ground = false

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

velocity.y += GRAVITY

if is_on_floor():
    on_ground = true
else:
    on_ground = false
    if velocity.y < 0:
        if gun_activated == false:
            $AnimatedSprite.play("Jump")
        elif gun_activated == true:
            $AnimatedSprite.play("Gjump")
    else:
        if gun_activated == false:
            $AnimatedSprite.play("Fall")
        elif gun_activated == true:
            $AnimatedSprite.play("Gfall")

velocity = move_and_slide(velocity, FLOOR)

THE BULLET SCRİPT

extends Area2D

const SPEED = 300
var velocity = Vector2()
var direction = 1

func _ready():
pass

func setbulletdirection(dir):
direction = dir
if dir == -1:
$AnimatedSprite.flip_h = true

func physicsprocess(delta):
velocity.x = SPEED * delta * direction
translate(velocity)
$AnimatedSprite.play("default")

func onVisibilityNotifier2Dscreenexited():
queue_free()

Godot version 3.4.2 win64
in Engine by (14 points)
if sign($Position2D.position.x) == 1:

is $Position2D.position.x always likely to be a positive number?

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.