[SOLVED] Player Rotation

I am doing a 2d project and I was wondering how I could force the model to rotate when the opposite button is pressed.

"For clarification I have one animation set for running and it works for when you want to travel right, however, I need assistance to figure out how to get the model to rotate a full 180 when you want to go left.

Hi @Overbaked and welcome,

Are you asking how to animate a smooth rotation? Or simply how to have your model face the other direction?

Also are you using physics to move your character or plain translations?

I would suggest either having an animation for that model, or if you can’t do that, use torque (if you’re using a dynamic rigidbody) or euler angles (if you’re using a non-dynamic one)

For simplicity sake I just want the model to face the other direction and as far as I know I’m using physics to move the character. I’m using a forked and modified version of the Third Person Controller.

I do have another question as well, I can’t seem to get the jump animation to play when the model is moving forward and the button is pressed. The impulse is still applied but the animation doesn’t play. I’m not sure if it’s an issue with order I’ve coded things or just a coding error in general.

Hi @Overbaked!

Can you explain a little better what you try to achieve and what your current result is?

If you share a link to your project it’s easier to help you with debugging.

2 Likes

https://playcanvas.com/project/814149/overview/2d-beta

You can rotate your model in your PlayerAnimationHandler script with the two rules below.

    this.entity.setLocalEulerAngles(0, 0, 0); 
    this.entity.setLocalEulerAngles(0, 180, 0);

Apart from that, I see that this script is not well constructed because your if statement is in many places the same as your else statement. The else statement in the example below will basically never execute because it is the same as the if statement before.

    if (a && !d) {

    } else if (a && !d) {
   
    }

This will make you lose the overview and also your jump function will not work as expected. The code below will solve the problems of this topic, but you’ll have to add the other animations in the right place yourself to make everything work the way you want.

    if (jump) {
        this.entity.animation.loop = false;
        this.direction = 'Jump';
    }
    else if (a && !d) {
        this.direction = 'Run';
        this.entity.setLocalEulerAngles(0, 0, 0);
    } 
    else if (d && !a) {
        this.direction = 'Run';
        this.entity.setLocalEulerAngles(0, 180, 0);
    }
2 Likes

Thank you so much for your help, I will probably come back with more questions on a separate thread but you were of much help. Thank you!

1 Like