[SOLVED] Rigidbody not working

Hi playcanvas community, I am having a problem regarding rigidbodies so I need some help.

The thing is my player rigidbody is the only one not colliding with other entities, I can’t figure out what is causing this since I have done all the things I know.

Here is the project link PlayCanvas | HTML5 Game Engine.

This is because you have moved the player to 0 on the Z axis every frame when all the objects are on 0.2:

    // Prevent player from moving out of screen and tilt when moving horizontally
    const position = self.entity.getPosition();
    const newPosition = new pc.Vec3(
        pc.math.clamp(position.x, xMin, xMax),
        pc.math.clamp(position.y, yMax, yMax),
        0);
    
    const newRotation = new pc.Vec3(0, 0, 0);
    this.entity.setPosition(newPosition);

As you are moving the player directly, I suggest changing the player to be kinematic instead. eg: https://playcanvas.com/editor/scene/1241028

If you still want to use dynamic rigid body, then I would change this to only teleport the player if they have crossed the boundaries rather than clamping every frame.

Thanks I reset every entity to 0 in z-axis but it brings another problem after colliding a few times with other entities the ship just disappears

I can’t reproduce that issue. I recommend debugging the app and looking at the position of the ship when it disappears

Ok I’ll do that., one more issue I have is that whenever I leave the game tab and return to it after some time all the entities spawned during that absence comes at one with no regard to physics they are just shoved in each other or they flicker weirdly. I know the game has a lot of bugs, I am new to playcanvas and learning so it is kinda difficult dealing with them all at once.

That’s because of the game controller using realtime to spawn asteroids rather than using the engine’s deltatime in update that is ‘paused’ if the tab is not in focus.

So I should move my code in the update function??

Edit: Not a good idea it seems

You should move the logic over but not the exact same code.

You are basically doing the same thing as this: Timers | Learn PlayCanvas

All right I think I get it now, Thanks for your help