This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hello, I'd like to know how I can flip a light2D node that I'm using as a flashlight for my 2d character.
I can't seem to find any explanations online on how I would go about this. My player script looks like this:

extends KinematicBody2D

var SPEED = 200
const GRAVITY = 20
const JUMP_POWER = -200
const FLOOR = Vector2(0, -1)
const TURNTIME = 0.1
const BASED_SPEED = 200

var velocity = Vector2()

var moving_right


var on_ground

func _physics_process(delta):

    if Input.is_action_pressed("ui_shift"):
        SPEED = 450
    if Input.is_action_just_released("ui_shift"):
        SPEED = BASED_SPEED

#Basic Movement

    if Input.is_action_pressed("ui_right"):
        velocity.x = SPEED
        moving_right = true
        $AnimatedSprite.flip_h = true

    elif Input.is_action_pressed("ui_left"):
        velocity.x = -SPEED
        moving_right = false
        $AnimatedSprite.flip_h = false

    else:
        velocity.x = 0

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


    if is_on_floor():
        on_ground = true
    else:
        on_ground = false


    velocity = move_and_slide(velocity, FLOOR)

I have my nodes as follows

KinematicBody2D
- Light2D
- AnimatedSprite
- LightOccluder2D
- CollisionShape2D

I am quite new to Godot and I for the life of can't figure out how to rotate everything.

I tried using

apply_scale(Vector2(-1, 1))

once but it just made my character spin out of control.

Any solutions or improvements would be much appreciated.

Godot version 3.3.2
in Engine by (13 points)

1 Answer

+1 vote

Make the light a child of the animatedsprite (This is not 100% necessary but it's convenient)

Remove the $AnimatedSprite.flip_h lines in your Input.is_action_presseds

Then add this somewhere in your _physics_process:

if velocity.x > 0:
     $AnimatedSprite.scale = Vector2(1,1)
elif: velocity.x < 0:
     $AnimatedSprite.scale = Vector2(-1,1)

Let me know how it goes!

by (311 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 Frequently asked questions and 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 [email protected] with your username.