Topic was automatically imported from the old Question2Answer platform.
Asked By
gyrosp
Hello everybody.
I’ve made a simple walking-cutout-animation.
In this animation the character is walking to the right. Is there any easy way to flip the cutout-animation, so the character is walking to the left?
I tried to change the x-value of the KinematicBody2D, but this leads to strange results (the animation is flipping continously).
This is my code so far (adapted from the demo-project ‘Platformer 2D’):
if (IsOnFloor()) {
if (mLinearVelocity.x < -SIDING_CHANGE_SPEED) {
mPlayerBody.Scale = new Vector2(Mathf.Abs(mPlayerBody.Scale.x) *-1f, mPlayerBody.Scale.y);
newAnimation = "Walking";
}
if (mLinearVelocity.x > SIDING_CHANGE_SPEED) {
mPlayerBody.Scale = new Vector2(Mathf.Abs(mPlayerBody.Scale.x), mPlayerBody.Scale.y);
newAnimation = "Walking";
}
}
Any help is really appreciated
Edit:
Here is a video of the strange behaviour.
Please don’t judge my Actor. It is just a simple stickmantest of cutout-animation and movement :):
Maybe the reference mLinearVelocity isn’t updated correctly? Just an assumption, since we don’t see where you create the reference. Also, since you don’t actually manipulate the y value of the vector, you can just say mPlayerBody.Scale.x *= -1.
Thanks for your answer :).
The reason I’m using Abs is, that I’m instancing the Actor with a different scale in the scene.
This is the whole script so far:
using Godot;
using System;
public class Player : KinematicBody2D
{
private const float SLOPE_SLIDE_STOP = 25.0f;
private const float WALK_SPEED = 250f; // pixels/sec
private const float SIDING_CHANGE_SPEED = 10f;
private Vector2 GRAVITY_VEC = new Vector2(0, 900);
private Vector2 FLOOR_NORMAL = new Vector2(0, -1);
private Vector2 mLinearVelocity = new Vector2();
private KinematicBody2D mPlayerBody;
private String mCurrentAnimation = String.Empty;
private AnimationPlayer mPlayerAnimation;
public override void _Ready()
{
mPlayerAnimation = (AnimationPlayer) GetNode("PlayerAnimation");
mPlayerBody = this;
}
public override void _PhysicsProcess(float delta) {
mLinearVelocity += delta * GRAVITY_VEC;
mLinearVelocity = MoveAndSlide(mLinearVelocity, FLOOR_NORMAL, SLOPE_SLIDE_STOP);
float targetSpeed = 0f;
if (Input.IsActionPressed("move_left")) {
targetSpeed += -1;
}
if (Input.IsActionPressed("move_right")) {
targetSpeed += 1;
}
targetSpeed *= WALK_SPEED;
mLinearVelocity.x = Mathf.Lerp(mLinearVelocity.x, targetSpeed, 0.1f);
String newAnimation = "Idle";
if (IsOnFloor()) {
if (mLinearVelocity.x < -SIDING_CHANGE_SPEED) {
mPlayerBody.Scale = new Vector2(Mathf.Abs(mPlayerBody.Scale.x) *-1f, mPlayerBody.Scale.y);
newAnimation = "Walking";
}
if (mLinearVelocity.x > SIDING_CHANGE_SPEED) {
mPlayerBody.Scale = new Vector2(Mathf.Abs(mPlayerBody.Scale.x), mPlayerBody.Scale.y);
newAnimation = "Walking";
}
}
if (mCurrentAnimation != newAnimation) {
mCurrentAnimation = newAnimation;
mPlayerAnimation.Play(mCurrentAnimation);
}
}
}