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.
+2 votes

So I was trying to make a patrolling AI and was using a RayCast2D to check if there is floor ahead but ran into an issue where if try to assign scale.x to one or negative one it would multiply it rather than set it equal to the value. I don't know if this is a glitch that is only on my machine because I got this working on my Surface perfectly fine.

in Engine by (60 points)

Please post the line of code which you use to scale.

if right == true:
    $RayCast2D.position = -RayPosition
    self.scale.x = -1
    motion.x = 100
elif right == false:
    $RayCast2D.position = RayPosition
    self.scale = 1

The $RayCast2D was i fixed the issue but this would cause the same problem.

motion = move_and_slide(motion, up)
    motion.y += 20
    if Input.is_action_pressed("ui_right"):
        self.global_scale.x = -4
    if Input.is_action_pressed("ui_left"):
        self.global_scale.x = 4

This was for testing purpose which caused the same issue.

elif right == false:
    $RayCast2D.position = RayPosition
    self.scale = 1 #should not be self.scale.x?

It should be but that doesn't change,

 if Input.is_action_pressed("ui_right"):
        self.global_scale.x = -4
    if Input.is_action_pressed("ui_left"):
        self.global_scale.x = 4

so that's not it.

Which type of node are you scaling? Can you reproduce this with a minimal scene?

The node that I'm scaling is a KinematicBody2D. The minimal scene would be were I set the inputs to assign the scaling.

 if Input.is_action_pressed("ui_right"):
        self.global_scale.x = -4
 if Input.is_action_pressed("ui_left"):
        self.global_scale.x = 4

Pressing left doesn't change the direction and pressing right will change the directions rapidly.

Is that your code really?

if Input.is_action_pressed("ui_right"):
        self.global_scale.x = -4
 if Input.is_action_pressed("ui_left"):
        self.global_scale.x = 4

# Are not they INVERTED? 
if Input.is_action_pressed("ui_right"):
        self.global_scale.x = 4 # since "ui_right" is positive?
 if Input.is_action_pressed("ui_left"):
        self.global_scale.x = -4

The Sprite and Ray is facing left to start with so scale.x = -1 would be right.

2 Answers

0 votes

I found out the issue. There seems to be a issue with the function moveandslide being called and assigning the x value. It probably has to do with how moveandslide handles scale with direction.

by (60 points)
0 votes

For those have issue purely with negative scale on X (not physics-specific issues), see my answer on https://godotengine.org/qa/92282/why-my-character-scale-keep-changing?show=146969#a146969

To sum up, scale.x = -1 is converted into scale.y = -1 and rotation_degrees = 180, but scale.x remains 1 internally, so next time you set scale.x = -1, it will try to flip again.

So the issue is about negative scale.x, which is 1 instead of the expected -1. This may give the impression that scale.x is multiplied by -1, but the test code suggested above with -4 and +4 should clearly show that it's not multiplying, else you'd have your sprite getting bigger and bigger.

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