How To Rotate Player Relative to Slope Angle

I did, the link is here: https://playcanvas.com/editor/scene/926034

@Leonidas Could you please debug my code to find the issue as soon as possible? Also, sorry for bothering you once again.

Hi @DevPlex01,

Since you are using a rigid body on the player entity you can’t use the setPosition/setRotation methods. The physics simulation overrides any custom translation we add.

In your case for the code to work you will have to find a way to do the raycast not from the Player downwards, but to whatever direction the ground is. Not sure how to do this in your game.

But since you are calculating angles manually, why don’t you use that angle to rotate the player as well? Try putting the following line at the end of your onContact() method:

this.entity.findByName('Animations').setEulerAngles(0,0, angle);

It will rotate the Player in most cases correctly, the rest I think you can figure out.

Oh right, I forgot. You see, I want to remove the fact I need to manually rotate the player. Because if i have a slope, I need the player to rotate accordingly. I’ll try and see if there’s a way to add rotation using the rigidbody. Also, I think we can simply raycast in different directions. Also, sorry for responding late. Timezone difference :slight_smile:.

1 Like

Ok, so i tried testing out the setRotation thing on the Animation child of the player. This was the result:

Was this supposed to happen, or is this just en error with my code?

You can’t use the code I’ve sent in your project as it is.

You need first to figure out a way to make a raycast from the player to the ground, wherever that is. When you make the propery raycast and you have the normal, then you can start working on finding the correct angles for the player.

Sorry @DevPlex01 I can’t be more of help here.

No problem. It’s a part of coding. I’ll figure it out eventually.

1 Like

Also, does .up mean up in local or world? Also would down in local mean the down according to the angle the entity is at?

@Leonidas?

It sorta works, it’s just the sprite:

I think I have a solution, but it is gonna be manually done.

entity.up is the entity’s local up axis in world space coordinates. pc.Vec3.up is the World’s local up axis (0, 1, 0).

1 Like