0
\$\begingroup\$

Im trying to make a bottle spawn when it is broken.

This is the code that makes that:

public class romperobjetos : MonoBehaviour {

public GameObject BrokenBottle;
public float breakVelocity = 5f;
public GameObject NoBrokenBottle;
public bool rompible = false;
public Vector3 oldPosition;
public Quaternion oldRotation;
public bool botellarota = false;


public void Start()
{

}

// Use this for initialization
public  void Update()
{    
    Debug.Log(NoBrokenBottle.GetComponent<Rigidbody>().velocity.magnitude);
    if (NoBrokenBottle.GetComponent<Rigidbody>().velocity.magnitude > breakVelocity)
    {
        rompible = true;
    }
    else  
    {
        rompible = false;
    }

}
public void OnTriggerEnter(Collider other)
{  
    if ((other.gameObject.name == "Plane") && (rompible))
    {
        oldPosition = NoBrokenBottle.transform.position;
        oldRotation = NoBrokenBottle.transform.rotation;
        Debug.Log("vamos bien");
        BreakBox();
    }
}
public void BreakBox()
{
    Instantiate(BrokenBottle, oldPosition, oldRotation);
    Debug.Log("se rompe");

        NoBrokenBottle.SetActive(false);
        botellarota = !botellarota;
}

}

The broken bottle prefab has a boolean variable that is true when it appears, but the unbroken bottle doesn't instantiate. This is the script for the spawn:

public class SpawnBotellas : MonoBehaviour {

public Transform[] spawnPoints;
public GameObject[] whatToSpawnPrefab;
public GameObject[] whatToSpawnClone;
public bool botella1rota = false;
// Use this for initialization
void SpawnAlgo()
{
    whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnPoints[0].transform.position, Quaternion.Euler(0, 0, 0));
}
 void Update()
{
    //botella1
    if (GameObject.Find("alidobas blanco roto").GetComponent<alidobasrota>().alidobasblanco)
    {
        Debug.Log("asdkj");
      //  whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnPoints[0].transform.position, Quaternion.Euler(90, 0, 0)) as GameObject;
    }

Sorry for my bad English and thanks.

\$\endgroup\$
0

2 Answers 2

0
\$\begingroup\$

We can simplify this into the following psuedocode.

public void OnTriggerEnter(Collider other) {
    if (gameObject.GetComponent<Rigidbody>().velocity.magnitude > breakVelocity) {
        // spawn new broken bottle object at current position
        // delete this game object
    }
}

Having the broken and unbroken bottle be seperate MonoBehaviours gives a number of advantages. One is keeping the logic for a broken and unbroken bottle seperate, reducing complexity. Another is that when addressing a broken or unbroken bottle, you can check object type instead of object state, this will likely result in less coupling.

\$\endgroup\$
0
\$\begingroup\$

I managed achieve it doing the script that instantoiates broken bottle instantiate the nice bottle too

public GameObject BrokenBottle;
    public float breakVelocity = 5f;
    public GameObject NoBrokenBottle;
    public bool rompible = false;
    public Vector3 oldPosition;
    public Quaternion oldRotation;
    public bool botellarota = false;
    public Transform[] spawnPoints;
    public GameObject[] whatToSpawnPrefab;
    public GameObject[] whatToSpawnClone;

    public void Start()
    {

    }

    // Use this for initialization
    public  void Update()
    {    
        Debug.Log(NoBrokenBottle.GetComponent<Rigidbody>().velocity.magnitude);
        if (NoBrokenBottle.GetComponent<Rigidbody>().velocity.magnitude > breakVelocity)
        {
            rompible = true;
        }
        else  
        {
            rompible = false;
        }

    }
    public void OnTriggerEnter(Collider other)
    {  
        if ((other.gameObject.name == "Plane") && (rompible))
        {
            oldPosition = NoBrokenBottle.transform.position;
            oldRotation = NoBrokenBottle.transform.rotation;
            Debug.Log("vamos bien");
            BreakBox();
        }
    }
    public void BreakBox()
    {
        whatToSpawnClone[0] = Instantiate(whatToSpawnPrefab[0], spawnPoints[0].transform.position, Quaternion.Euler(-90, 0, 0)) as GameObject;
        Instantiate(BrokenBottle, oldPosition, oldRotation);
        Debug.Log("se rompe");

            NoBrokenBottle.SetActive(false);
            botellarota = !botellarota;
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .