0 votes

I'm a Artist defo not a coder so need some help with the code it does not compile could some one knowledgeable help me out

extends KinematicBody2D

const MOVE_SPEED = 500

Jump

export var fallgravityscale := 150.0

export var lowjumpgravity_scale := 100.0

export var jump_power := 500.0

var jump_released = false

Physics

var velocity = Vector2()

var earth_gravity = 9.807 # m/s^2

export var gravity_scale := 100.0

var on_floor = false

func physicsprocess(delta):

var move_dir = 0

if Input.is_action_pressed("rightmove"):
move_dir += 1

if Input.is_action_pressed("leftmove"):
move_dir -= 1

moveandslide(Vector2(movedir * MOVESPEED, y_velocity), Vector2(0, -1))

if Input.is_action_just_released("jump"):
jump_released = true

Applying gravity to player

velocity += Vector2.DOWN * earthgravity * gravityscale * delta

Jump Physics

if velocity.y > 0: #Player is falling

Falling action is faster than jumping action | Like in mario

On falling we apply a second gravity to the player

We apply ((gravityscale + fallgravityscale) * earthgravity) gravity on the player

velocity += Vector2.DOWN * earthgravity * fallgravity_scale * delta

elif velocity.y < 0 && jump_released: #Player is jumping

Jump Height depends on how long you will hold key

If we release the jump before reaching the max height

We apply ((gravityscale + lowjumpgravityscale) * earth_gravity) gravity on the player

It result on a lower jump

velocity += Vector2.DOWN * earthgravity * lowjumpgravityscale * delta

if on_floor:

if Input.isactionjust_pressed("jump"):

velocity = Vector2.UP * jump_power #Normal Jump action

jump_released = false

velocity = moveandslide(velocity, Vector2.UP)

if isonfloor(): on_floor = true

else: on_floor = false

asked Nov 26, 2019 in Engine by debarstard (12 points)

could you please put it more friendly like this? There is a button with brackets at the top of the editor..

extends RigidBody2D


export (int) var kick_force = 300


onready var arrow_kick = $arrow_kick


func _ready():
    GLOBAL.kicker = self


var physics_linear_velocity
func _integrate_forces(state):
    if physics_linear_velocity:
        linear_velocity = physics_linear_velocity
        physics_linear_velocity = null


func kick (direction, kick_scale):
    print(kick_scale)
    physics_linear_velocity = direction.normalized() * kick_force * kick_scale


func point_at(direction):
    arrow_kick.point_at(direction)

1 Answer

0 votes

I can at least provide you my player's moving script. What I also have added to mine is the code to play certain animations with the movement of the character. I also added the code for the player to shoot if want to use that.

const SPEED = 250
const GRAVITY = 10
const JUMP_POWER = -500
const FLOOR = Vector2(0, -1)

const PLASMABLAST = preload("res://Scenes/PlasmaBlast.tscn")

onready var World = get_node("/root/game")

var velocity = Vector2()

var jump = false
var jump_speed = 10
var on_ground = false
var can_move = true
var is_attacking = false

func _physics_process(delta):
    if Input.is_action_pressed("forward"):
        if is_attacking == false || is_on_floor() == false:
            velocity.x = SPEED
            if is_attacking == false:
                $AnimatedSprite.play("run")
                $AnimatedSprite.flip_h = false
                if sign($Position2D.position.x) == -1:
                    $Position2D.position.x *= -1
    elif Input.is_action_pressed("backward"):
        if is_attacking == false || is_on_floor() == false:
            velocity.x = -SPEED
            if is_attacking == false:
                $AnimatedSprite.play("run")
                $AnimatedSprite.flip_h = true
                if sign($Position2D.position.x) == 1:
                    $Position2D.position.x *= -1
    else:
        velocity.x = 0
        if on_ground == true && is_attacking == false:
            $AnimatedSprite.play("idle")

    if Input.is_action_pressed("jump"):
        if is_attacking == false:
            if on_ground == true:
                velocity.y = JUMP_POWER
                on_ground = false
                $AnimatedSprite.play("jump")
    if Input.is_action_just_pressed("shoot") && is_attacking == false:
        if is_on_floor():
            velocity.x = 0
        is_attacking = true
        $AnimatedSprite.play("attack")
        var plasmablast = PLASMABLAST.instance()
        if sign($Position2D.position.x) == 1:
            plasmablast.set_plasmablast_direction(1)
        else:
            plasmablast.set_plasmablast_direction(-1)
        get_parent().add_child(plasmablast)
        plasmablast.position = $Position2D.global_position

    velocity.y += GRAVITY
answered Dec 7, 2019 by ItsYoBoi (50 points)
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.