0 votes

This is my 2nd day using godot and making games in general. I took some movement code from a video on yt and made a single sprite for my character (not animated, for now) and it moves okay but when i turn left the sprite changes its scale to -1 and so does the collision box (probably) but the collision box goes out of the character to the right so an arm goes through tiles. I tried to find an answer but i couldnt find anything thats why i created an account here to ask.

here is the script:

 extends KinematicBody2D

const GRAVITY = 35
const JUMP_FORCE = 550
const SPEED = 300
const ACCEL = 0.4
const MAXACCEL = 800

onready var sprite = $Sprite

var velocity = Vector2.ZERO

func _unhandled_input(event):
    if event.is_action_pressed("jump") && is_on_floor():
        velocity.y = -JUMP_FORCE    

func _physics_process(_delta):
    velocity.y += GRAVITY
    if velocity.y > MAXACCEL:
        velocity.y = MAXACCEL

    var dir = Input.get_action_strength("right") - Input.get_action_strength("left")
    if dir != 0:
        sprite.scale.x = dir

    velocity.x = lerp(velocity.x, SPEED * dir, ACCEL)
    velocity = move_and_slide(velocity, Vector2.UP)
Godot version 3.4.1
in Engine by (12 points)

1 Answer

0 votes

Your tree should look like this.

-KinematicBody2d
--CollisionShape2d
--Sprite

When you want to flip your sprite do.

$sprite.scale.x = -1

If your CollisionShape2d is asymmetric then do this instead.

-KinematicBody2d
--CollisionShape2d
---Sprite

$CollisionShape2d.scale.x = -1

by (559 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.
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.