How to prevent objects from going through each other

Here’s my project link - PlayCanvas 3D HTML5 Game Engine

I’m trying to make a rolling ball game where you press buttons around the map and then beat the level. I can’t figure out how to make the player not go through obstacles, though. The only tutorials I’ve found say how to do x when y touches z, but I want to prevent y and z from touching.

Hi @Bk_Gaming,

The easiest way is to add collision and rigidbody components and have the physics engine automatically handle collisions.

Check this tutorial on how to get started with that:

Also, have you check the rolling ball example project that is automatically added to your account on creation? It showcases exactly that, how to roll a ball using physics.

I can’t seem to find a link to that project though (@yaustar :smiley: ), so here is one from a random user:

I’ve tried collision and rigidbody components with the tutorial in Playcanvas documentation, but it was the same.

So in your project you are using the pc.Entity translate method to move your entity.

That can work for some games but if you are looking to have your entity interact with other bodies through physics, then you should be using forces/impulses to move your entity.

Do read the tutorial I’ve shared above, First Person Movement, it showcases with exact steps that.

I’ll try that with a Dynamic rigid body, thanks.

1 Like

I changed the code to use forces based on the rolling ball project, but get this error on launch

[[movement.js?id=64390113&branchId=9b3e4e8d-ba94-49a1-91fd-2e2e8535c26d:41]]( Cannot set properties of undefined (setting 'x')

TypeError: Cannot set properties of undefined (setting 'x')
at scriptType.Movement.update (
at ScriptComponent._scriptMethod (
at ScriptComponent._onUpdate (
at ScriptComponentSystem._callComponentMethod (
at ScriptComponentSystem._onUpdate (
at (
at Application.update (
at Application.tick (
at Application.start (

Edit: Error is gone but the player won’t move and the camera is going everywhere.

Hey, testing your project I can see that the ball moves as expected.

The problem is that the camera rotates with the ball, which is expected since it’s a child of the Sphere entity.

Trying putting somewhere else in the hierarchy and add a script to position based on the player’s position.

1 Like