Adding mesh collider though script

Hey guys, im trying to add a mesh collider to my created object through this code :

my asset is added in inspector

but still not showing the asset, when i manually try to add a mesh collider it works, but whenever i try to do that using script it is not working. do you see any issues that might causing this problem ?

The code looks syntetically correct. The only thing missing is adding the newly created entity to the scene, e.g. this.app.root.addChild(newBox);. I would also highly recommend to google javascript coding conventions and pick a common one. Your current style is very hard to read.

1 Like

Actually in other part of code there is parenting already defined

image

At least its hard to copy for people :smiley:

Well, not really - no matter how you mangle your code, most of editors can auto-format the code to look normal, so you only making it worse for yourself, picking bad habbits early.

As for the collision issue - the code is correct and it will add a mesh collider. You have to look elsewhere for the problem. I would look into testing that the node you are adding the entity to is correct and that the collision mesh is fine.

1 Like

Node is fine as i tried to add a box collision which showed up on the place

Only mesh colliders are not shown up, but when i try manualy to add a mesh collider everything works fine

You can double check that the script was parsed and the new fields are used (parse button in editor). Apart from that, it is hard to say from the info you have shown. Perhaps you could make a simple project to demo - it would make it easier.

https://playcanvas.com/editor/scene/1064928

here is the scene, if you want to check the script, see trapColliders in hierarchy

I must search all the enttities in the scene to find that trapColliders and then find the corresponding script and anything related to the problem you have. This project is too big for a quick look. Can you make a new empty project with a single plane and a model to demonstrate the problem?

Here is a simple scene where you can see the problem.

Here i have objects i want attach my mesh collider on through script.

In hierarchy i have my trapColliders, those are objects where the script for creating colliders is attached.

when you click on the object you will see inspector properties, and there are script attributes where you can attach your mesh collider

But when i attach my mesh collider through script, it does not appear in the game(i also have collision ingame drawings so you can see collisions)

Here is the script that creates a collisions: https://playcanvas.com/editor/code/753870?tabs=40934283

A quick point: when i try to add manually mesh collider it works,but i want it to be added through script(that means there is no problem with model of collider)

Anybody ? :slight_smile:

I’ve had a look into your project the other day. There is indeed an issue with the mesh collider not following the animation node for some reason. Attaching some primitive mesh works fine, so I assumed there must be a bug. Your project is pretty large and complex for some simple tests, so I didn’t spend much time though. I was planning to make a blank project to try to reproduce the issue during the weekend. You can help creating one. If the issue can be easily reproduced, we can file a bug report and the team can look at it.

Im going to create simple blank project with the scripts needed to test the mesh colliders following… its really hard to find an issue in such big project though, too many scripts working at the same time and even a single line of code could be causing the issue. but i dont know if it is the problem of any scripts… lets try the test scene then, thanks for taking a look @LeXXik

1 Like

Here I have created a simple project with no additional script, only those needed:

https://playcanvas.com/editor/scene/1070979 <- link for the editor
https://launch.playcanvas.com/1070979?debug=true <- link for the game

No mesh collider was created even in the new blank project with just two active script

EDIT. I have a draw collision script attached so all collisions are visible ingame.

Thank you for making a sample project. I’ve made a minimal repro case here:
https://playcanvas.com/editor/scene/1071300

It looks like a valid problem, so I’ve also made a new github issue:

Still havent found a fix for this, anybody tried ?

Could that be an engine bug?

Maybe. It’s going to need deeper investigation with LeXXiks example: https://github.com/playcanvas/engine/issues/2744

Its been a month now and yet nobody responded :confused: Did anybody come up with something ?

Finally had a chance to revisit this. Looks like the docs are wrong and the asset field when creating a collision component has to be a number (the asset id), not a pc.Asset type.

Example here: https://playcanvas.com/project/768833/overview/adding-mesh-collider-runtime

Docs fix here: https://github.com/playcanvas/engine/pull/2915

Edit: Docs were not wrong, the orientation of the shape in the game code was incorrect.

1 Like