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:

https://developer.playcanvas.com/en/tutorials/first-person-movement/

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:

https://playcanvas.com/project/530412/overview/my-first-project

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]](https://playcanvas.com/editor/code/865734?tabs=64390113&line=41&col=18&error=true): Cannot set properties of undefined (setting 'x')

TypeError: Cannot set properties of undefined (setting 'x')
at scriptType.Movement.update (https://launch.playcanvas.com/api/assets/files/movement.js?id=64390113&branchId=9b3e4e8d-ba94-49a1-91fd-2e2e8535c26d:41:18)
at ScriptComponent._scriptMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:64341:19)
at ScriptComponent._onUpdate (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:64386:11)
at ScriptComponentSystem._callComponentMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:64951:49)
at ScriptComponentSystem._onUpdate (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:64968:9)
at ComponentSystemRegistry.fire (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:758:18)
at Application.update (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:71486:17)
at Application.tick (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:72096:16)
at Application.start (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.50.2:71459:9)
at https://launch.playcanvas.com/editor/scene/js/launch.js:14548:25

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