0 votes

I am making a 2d Game using godot 3.2 stable release, I want me Player to flip it's direction along with it's collision shapes, Only possible way of doing so is by fliping Entire kinematicsbody2D. But when doing so It behaves Strangely. Here's a video of that
Strange Behaviour

here's my script of doing so

    extends Actor

onready var _anim : AnimatedSprite = get_node("AnimatedSprite")

func _enemy_kills_me(body : PhysicsBody2D) -> void:
    queue_free()

func _killed_the_enemy(area : Area2D) -> void:

    _velocity.y = -maxSpeed.y / 2

func _process(delta):
    animPlayer()



func _physics_process(delta :float) -> void:
    var is_jumping = Input.is_action_just_released("jump") and _velocity.y < 0.0
    var Direction = Get_Direction()
    _velocity = Calcule_Jump_Height(Direction, _velocity, is_jumping)
    _velocity = move_and_slide(_velocity, Vector2.UP, true)

func Get_Direction() -> Vector2:
    var x = 0.0
    if Input.is_action_pressed("move_right"):
        x = 1.0
    elif Input.is_action_pressed("move_left"):
        x = -1.0
    else:
        x = 0.0

    if x != 0.0:
        scale.x = x * 0.75
    var y = 1.0

    var jumping := false
    if Input.is_action_just_pressed("jump") and is_on_floor():
        y = -1.0
    else:
        y = 1.0
        jumping = false

    return Vector2(x,y)

func Calcule_Jump_Height(
        dir :Vector2,
        linearVelocity :Vector2,
        is_Jumping : bool
) -> Vector2:
    var new_Velocity := linearVelocity
    if dir.x > 0.0:
        new_Velocity.x = min(new_Velocity.x + (speed * get_physics_process_delta_time() * 4) , maxSpeed.x)
    elif dir.x < 0.0 :
        new_Velocity.x = max(new_Velocity.x - (speed * get_physics_process_delta_time()  * 4), - maxSpeed.x)

    new_Velocity.y += Gravity * get_physics_process_delta_time()

    if dir.y == -1.0:
        new_Velocity.y = maxSpeed.y * dir.y 
    if is_Jumping:
        new_Velocity.y = 0.0

    if is_on_floor():
        if !Input.is_action_pressed("move_left") and !Input.is_action_pressed("move_right"):
            if new_Velocity.x < -100.0:
                new_Velocity.x += (speed * get_physics_process_delta_time() * 4)
            elif new_Velocity.x > 100.0:
                new_Velocity.x -= (speed * get_physics_process_delta_time() * 4)
            else:
                new_Velocity.x = 0
    if !is_on_floor():
        if !Input.is_action_pressed("move_left") and !Input.is_action_pressed("move_right"):
            if new_Velocity.x < -100.0:
                new_Velocity.x += (speed * get_physics_process_delta_time() / 2)
            elif new_Velocity.x > 100.0:
                new_Velocity.x -= (speed * get_physics_process_delta_time() / 2)
            else:
                new_Velocity.x = 0
    return new_Velocity

func animPlayer() -> void:
    if _velocity.x != 0.0 and _velocity.y == 0.0:
        _anim.play("Run")
        _anim.position.y = -109.105
    elif _velocity.y < 0.0:
        _anim.play("GoingUp")
        _anim.position.y = -82.0
    elif _velocity.y > 0.0:
        _anim.play("CommingDown")
        _anim.position.y = -90.0
    else:
        _anim.play("Idle")
        _anim.position.y = -109.105

i Used this code to flip

unc Get_Direction() -> Vector2:
var x = 0.0
if Input.is_action_pressed("move_right"):
    x = 1.0
elif Input.is_action_pressed("move_left"):
    x = -1.0
else:
    x = 0.0

if x != 0.0:
    scale.x = x * 0.75 # to flip character
var y = 1.0

var jumping := false
if Input.is_action_just_pressed("jump") and is_on_floor():
    y = -1.0
else:
    y = 1.0
    jumping = false

return Vector2(x,y)
in Engine by (67 points)

1 Answer

+1 vote
Best answer

I'd think you could parent all of your existing Player nodes to a single, Node2D container, and simply flip that node to get the effect you want (via scale.x = 1 or scale.x = -1).

by (10,756 points)
selected by

thanks, it worked :)

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.

Categories