C# script to make object shatter into smaller objects.

Content not available.
Please allow cookies by clicking Accept on the banner

Fristly add Cube and attach Rigidbody and below script to it. After that create empty GameObject, reset it’s position and add cubes with Rigidbodies to match the cube that was added previously. Next, save that GameObject as a prefab and add it to the script of the main cube. After that simply call the DestroyObject function from the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// ? 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class DestroyableObject : MonoBehaviour {
    public GameObject fragments;

    private void OnMouseDown()
    {
        Instantiate(fragments, transform.position, transform.rotation);
        gameObject.SetActive(false);
    }
}

 



Unity C# Destroyable GameObject //Fragmenting into smaller objects
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close