I have a 3D character with rigid body
I am using W to move forward while S to back
now I want to rotate 180 when I press S once but nothing is happening
In a nutshell, if you are using a dynamic rigid body, you cannot set the position or rotation of the entity as that is controlled by the physics simulation.
A common workaround in games is to have the visual model as a child of the rigid body and rotate that instead.
I wouldn’t use teleport on a dynamic object as that cancels out forces on the rigidbody.
JS vs C# and differences in the structures of how scripts are created. In your case, putting it ‘outside’ the script functions is putting it into global scope of the browser window.
JS is a dynamic language so you can add properties, functions etc to objects at any time. In the initialize function, it’s common practise to add the properties you need for the lifetime of the script in the this object.
We should expand on that. Yes, it should be used on dynamic rigidbodies but only for the purposes of teleporting. Not continuous movement or rotation which is what you are doing.
The function doesn’t work for kinematic or static rigidbodies.
When I say teleporting, I mean like reseting the player position after falling off a level.
The issue you are running into is that the anim component animates the character by changing the transform of the bone entities.
Your code for character rotation is also trying to change the transform as well. As the anim component update is down after scripttype update (but before scripttype postUpdate, see Application Lifecycle | Learn PlayCanvas), the entity transform is set to whatever the anim component uses.
As mentioned earlier and what is done in the crash course that I sent you is the recommended path to handle this, which is to set the character as a child of the rigidbody. In the anim component, you can set the Root Bone to be character model entity.