Looking for implementation advice

I’m still very green in Unity and I’m looking for the best way to do the following in the Unity system…

For learning purposes I’m building a clone of the game “The Incredible Machine”. If you Google for it you’ll see what I’m after, but it’s essentially a game where you build wacky machines to get some object from point A to point B using silly objects like hammers and cats, etc. It’s actually pretty entertaining.

Anyway, I have a lot of the parts ready and I can build levels in the Unity interface, but what I need to implement is way for people to have their own level builder - which means people can position objects and then have a “start” button. So, I need to enable user to take objects, place them on the game surface, and the click a START button to basically turn on the physics engine. An example would be where a person moves a stone to a certain place (where it would kick start the rub goldberg machine), but it won’t fall until the person hits “Start”.

What is the best way to implement this “Start” button functionality? To let people position objects in the scene, but to turn off the physics engine until I say “Start”? I looked at the Rigidbody2d component and it doesn’t seem to have an *.enabled property…so is there a way to enable/disable this?

Looking for more seasoned advice. Thanks in advance.

I’m by no means seasoned, but I thought this was pretty interested so I did some testing. It looks like one thing you could do is have a script grab all the objects and toggle the “isKinematic” attribute, which disables normal physics on the object. I made a script to do this in my game and it seemed to work. Here’s that script:

using UnityEngine;
using System.Collections;

public class PhysicsToggle : MonoBehaviour {

    bool physicsActive = true;

    public void PhysicsToggle(){
        Rigidbody2D[] allObjects = FindObjectsOfType<Rigidbody2D> (); // Grab all Rigidbody2D objects in the scene

        physicsActive = !physicsActive; // Toggle the physicsActive variable
        foreach (Rigidbody2D theRigidbody2D in allObjects) { // Go through each Rigidbody2D that we found and set isKinematic
            theRigidbody2D.isKinematic = physicsActive;
        }
    }
}

Probably instead of toggling isKinematic you could freeze all the constraints with something like this inside the foreach loop:

theRigidbody2D.constraints = RigidbodyConstraints2D.FreezeAll; // Freeze all movement
theRigidbody2D.constraints = RigidbodyConstraints2D.None; // Allow all movement

I’m a big fan of The Incredible Machine, lots of good memories!

1 Like