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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Is there a way to make an object split up into 2 objects while in game?

Discussion in 'Scripting' started by x_puppet_xmaster, Dec 29, 2017.

  1. x_puppet_xmaster

    x_puppet_xmaster

    Joined:
    Oct 21, 2017
    Posts:
    3
    I'm very new to scripting but I was wondering if there's a way to get an object. while in game, to become split into 2 objects? My goal is to have a mechanic for an object to be able to split up into multiple pieces but to also be able to return back to its original state.

    Ultimately, I'd like for the size of the objects to become smaller when splitting up and when they return, they revert to their original size. But I have no idea how to go about it.
     
  2. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,459
    Splitting like the object was exploding? Or splitting into copies of itself?
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    A simple way would be to build your gameobject around several child objects. When you want to split up the "pieces" you can unparent the child objects. When you want to recombine them, you can parent them again and will probably want to set their local positions so they are in the correct place relative to the parent.

    For example you could have a "gun" object made up of the receiver, barrel, optics, stock, magazine, etc. You could unparent the optics, have your character move it somewhere else, possibly replace it with a new optics object, that you then parent to the gun object. Like when you want to switch the gun from a red dot to a long range scope.

    As for messing with the size, the easiest way is to just change the scale of the object.

    If what you mean is you want to break up a single mesh, there's been several threads discussing it on the forum.
     
  4. x_puppet_xmaster

    x_puppet_xmaster

    Joined:
    Oct 21, 2017
    Posts:
    3
    splitting into copies of itself
     
  5. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    4,459
    Instantiate seems to be your friend then. :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can scale your objects down, and return them to a scale of 1,1,1 (or whatever their original) when needed.

    You can consider object pooling if you have only a few types of objects and they do this regularly. (instead of destroying / instantiating).