https://playcanvas.com/editor/scene/1475376
In playerMovement.js
, line 49, I have put some code to turn the player when I press the left key or the “a” key. It doesn’t seem to work, and it only turns when I hold the button down while it is falling and teleported back up. I don’t know why this happens. Could somone help me with this issue?
dynamic body is not work with setEulerAngles
try “rigidbody.applyTorque” or make your model, render component to children of dynamicbody
1 Like
@jejelee98 rigidbody.applyTorque
doesn’t seem to work either
Hi @InfinityLight! The Angular Factor is set to 0
. Can you try to change this value? I think only the Y axis should be enough.
1 Like
Thank you @Albertos, it is rotating now, but it continually rotates if I hold down the button. Is there a way to make it only face one way if I press a button? For example, if I press the left arrow key, it should face towards the left only.
You can try to replace isPressed
with wasPressed
.
1 Like
That still makes it rotate continually if the user repeatedly presses the button. I don’t know much about quaternions, and the docs felt too confusing. How can I make it face one direction with no attention to the user pressing the button how many times they do?
I expect that the player rotates 90 degrees everytime the user press the button. Is that the case? If not, then maybe teleporting is an option?
https://developer.playcanvas.com/api/pc.RigidBodyComponent.html#teleport
I see in the docs that teleport
uses quaternion. I am not very familiar with them and I found that the docs were too confusing. Is there another way to do this (suppose by using the world rotation), or is there a way to convert world rotation into a quaternion?
The way below works for me.
// initialize funtion
this.pos = this.entity.getPosition();
this.rot = this.entity.getEulerAngles();
// update funtion
if (this.app.keyboard.wasPressed(pc.KEY_LEFT) || this.app.keyboard.wasPressed(pc.KEY_A)) {
this.rot.y += 90;
this.entity.rigidbody.teleport(this.pos, this.rot);
}
if (this.app.keyboard.wasPressed(pc.KEY_RIGHT) || this.app.keyboard.wasPressed(pc.KEY_D)) {
this.rot.y -= 90;
this.entity.rigidbody.teleport(this.pos, this.rot);
}
The next problem is that the force is not applied in the right direction after rotating. You need to apply the force in the forward direction of the entity. You can check the third person example project to see how that’s done.
https://developer.playcanvas.com/en/tutorials/third-person-controller/
I tried inserting the code but some weird things happened. The player only moved once I released a key, and for some reason when I press the S key or the down key it vibrates back and forth.
The first two lines I shared here need to be in the initialize function. Not in the update function.
The same problems still occur even if I changed it
Because you didn’t remove it from the update function.
Now that I removed it, it still happens.
Your code is not the same anymore as I shared here. (Line 3 and 8).
No, I would like it to face only to the left no matter how many times the user presses a key. I seem to have figured out a way. Thanks for your help though!
Ah, finally I got it. Then the changes you made for line 3 and 8 are indeed okay! 