[SOLVED] How do we set the collision property with code

How can I replace the “collision” property of an object, the “sphere” attribute of type “radius” with code?

For example, we can set the size of the object with the code
how do I do this for collision?

Hi @Onur_Ozturk! That should be something like below.

this.entity.collision.radius = 1;
1 Like

Hi, thank you it works. However, when I update the radius size, the speed of the object instantly becomes 0. How can I prevent this?

Most likely because changing the radius recreates the physics body. You could copy the linear and angular velocity of the body before making the change, and reapply it afterwards.



Controller.prototype.update = function (dt) {
    var moveVect = new pc.Vec3(moveVert, 0, moveHoriz);
    var direction_moveVect = new pc.Vec3(direction_moveVert, 0, direction_moveHoriz);


Hello, thank you for your reply. However, I am constantly applying a force inside the “update” function.

Is there a way to change the radius without rebuilding the physics body?

No I don’t think so.

But changing the radius won’t affect your update method I think. It will execute at that exact point. Try it.

1 Like

Let me explain why I want this kind of thing. When the top left bar is full, I activate random features. When the sphere gets bigger and I update the collision area, the ball stops momentarily, then even if it continues to regain speed, the probability of falling down the tracks is very high because its speed is slow. How can we apply a solution to this?

I can’t test it right now, but what I imagined is this:

const linear = entity.rigidbody.linearVelocity.clone();
const angular = entity.rigidbody.angularVelocity.clone();

this.entity.collision.radius = 1;

entity.rigidbody.linearVelocity = linear;
entity.rigidbody.angularVelocity = angular;
1 Like

It worked oddly enough. My logic has failed here. :smiley: Thank you.

1 Like