How to rotate 3D model with rigid Body

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

I am trying to rotate on model axis instead of rigid body

var CharacterContorller = pc.createScript('characterContorller');

CharacterContorller.attributes.add('speed',{type:'number'});

// initialize code called once per entity
CharacterContorller.prototype.initialize = function() {
     this.entity.rotate(0,90,0);
   
   

};

// update code called every frame
CharacterContorller.prototype.update = function(dt) {

     if(this.app.keyboard.wasPressed(pc.KEY_W)&&(this.entity.anim.baseLayer.activeState !=='Walking'))
    {
        this.entity.anim.setBoolean("Walk",true);
    }
 if(this.app.keyboard.wasReleased(pc.KEY_W)&&(this.entity.anim.baseLayer.activeState =='Walking'))
    {
        this.entity.anim.setBoolean("Walk",false);
        
    }

    //MovementRigidBody
    if(this.app.keyboard.isPressed(pc.KEY_W))
    {
        
        this.entity.rigidbody.applyForce(0,0,10*this.speed*dt);
    }

      if(this.app.keyboard.isPressed(pc.KEY_S))
    
      { this.entity.rigidbody.applyForce(0,0,-10*this.speed*dt);
    }

    if(this.app.keyboard.wasPressed(pc.KEY_S))
    {
      this.entity.rotate(0,180,0);
    }


}

;

I tested out disabling the collision and it works then but I need to have collision on for detection.please suggest me solution

I also created an attribute to rotate child rig and model instead but that didn’t workout too

Hi @Nofil_khan!

I assume you use a dynamic rigidbody? In that case you can’t rotate the entity directly. You can rotate the rigidbody of the entity using teleport().

https://developer.playcanvas.com/en/api/pc.RigidBodyComponent.html#teleport

1 Like

I want to just rotate on Y axis,how would I write this whole function, I mean how to give entity the position and X, Z rotation as it has already

I’m not totally sure, but I think you can try something like below.

var pos = this.entity.getPosition();
var rot = this.entity.getEulerAngles();
this.entity.rigidbody.teleport(pos.x, pos.y, pos.z, rot.x, rot.y + 180, rot.z);
1 Like

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.

You can see it being done here at the 47min mark of setting up the physics: Crash Course - Make a Game | Learn PlayCanvas

I got it working as I wanted using a teleport
image

I am a little confused about variables in PC.

Normally in unity, you can declare variables outside both initialize and update and they would work

but in play canvas, you have to declare a variable in either update or initialize to get them working .? Can you guide me a little bit about them

Also here is the video of my character now working perfectly

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.

I recommend reading some material on JS like You-Dont-Know-JS/README.md at 1st-ed · getify/You-Dont-Know-JS · GitHub

image
But PC manual says to use it with a dynamic rigid body only?

Thanks for help with varaibles

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.

1 Like

Actually, I did another test project,
I added an attribute and referenced the entity

then on the parent with rigid body and collider, I added this script and then used a character skeleton in that attribute,I got what I was looking for

BUT HERE IS THE PROBLEM IT ONLY WORKS IF THERE IS NO ANIM COMPONENT AS AS SOON I ADD ANIM COMPOENT ,IT STOPS WORKING WHY IS THAT

here

Please post a public example project of the issue, it’s much easier to investigate :slight_smile:

1 Like

Here we go

https://playcanvas.com/editor/project/1056749

Note: Only By removing anim component or switching it off ,we can use S or A to change directions

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.

ie:

Project: https://playcanvas.com/project/1057111/overview/f-rigidbodyrotate

The Crash Course covers a lot of what you are trying to do here so it’s worth a watch if you haven’t already.

1 Like

Thanks a lot ,I think I got it working will share updates here