[SOLVED] Can you toggle on and off collisions and rigidbodies?

I am making an obby-like thing and I want to have panels that have a collision and a rigid body that turns off and on.

Hi @Codeknight999!

You can access the component of an entity like below.

this.entity.collision.enabled = false;
this.entity.rigidbody.enabled = false;
this.entity.collision.enabled = true;
this.entity.rigidbody.enabled = true;

Thank you, again! Do you also know how to do a random 1 or 2 code?

I don’t understand what you mean. Is it related to this topic?

Kind of, it has to do with the obby, I want it to be random of one or 2 on start so then I can store it as a variable, and then I can use an if statement to turn on and off the collision and rigid body.

Sorry, I also don’t know what is ‘obby’. Do you want to get random a value 1 or 2?

To put it in simple terms, I want a variable that at the start of the run is a random number of one or two. Based on the 1 or 2 panels in my obby( it means obstacle course) the panels will have a collision and rigid body or not.

Math.random() gives a value between 0 and 1.

// initialize
this.value = 0;

if (Math.random() < 0.5) {
    this.value = 1;
}
else {
    this.value = 2;
}

1 Like

Thank you a lot! You are always helpful when it comes to code :smile:

3 Likes