Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Looking for implementation advice

Discussion in '2D' started by kwende, Jul 19, 2015.

  1. kwende

    kwende

    Joined:
    Jul 10, 2015
    Posts:
    2
    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.
     
  2. Zk

    Zk

    Joined:
    May 25, 2013
    Posts:
    19
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PhysicsToggle : MonoBehaviour {
    5.  
    6.     bool physicsActive = true;
    7.  
    8.     public void PhysicsToggle(){
    9.         Rigidbody2D[] allObjects = FindObjectsOfType<Rigidbody2D> (); // Grab all Rigidbody2D objects in the scene
    10.  
    11.         physicsActive = !physicsActive; // Toggle the physicsActive variable
    12.         foreach (Rigidbody2D theRigidbody2D in allObjects) { // Go through each Rigidbody2D that we found and set isKinematic
    13.             theRigidbody2D.isKinematic = physicsActive;
    14.         }
    15.     }
    16. }
    Probably instead of toggling isKinematic you could freeze all the constraints with something like this inside the foreach loop:

    Code (CSharp):
    1. theRigidbody2D.constraints = RigidbodyConstraints2D.FreezeAll; // Freeze all movement
    2. theRigidbody2D.constraints = RigidbodyConstraints2D.None; // Allow all movement
    I'm a big fan of The Incredible Machine, lots of good memories!
     
    kwende likes this.