Creare Videogiochi - Game Developer

Versione completa: Rottura vetro o altri oggetti all'arrivo del proiettile
Al momento stai visualizzando i contenuti in una versione ridotta. Visualizza la versione completa e formattata.
Ciao ragazzi, come da titolo vorrei sapere come creare uno script che "capisca" quando il proiettile impatta sul box collider dell'oggetto.

Concentriamoci ad esempio su un vetro, ho scaricato un asset gratuito che da già un vetro diviso in diversi pezzi, ma composto intero, ha già uno script interno che al click del mouse sul vetro, si rompe, con anche il rumore.

Ho provato a giocare un po con lo script per fargli capire che non si dovesse rompere al click del mouse ma solamente all'arrivo di un proiettile...ma non ci sono riuscito!

Voi avete idee su come lavorare a questo?

Direi che lo script poi potrebbe essere utilizzato anche per altri oggetti, per esempio casse, tavoli etc, come in tutti giochi moderni!

Idee? Io sto usando i raycast per lo sparo
Uhm, prima di tutto dovresti postare lo script. Poi per fare in modo che si distrugga quando lo sparo lo tocca, lo sparo deve avere un collider con una tag in modo che nello script scriverai:

if(other.collider.tag == "Sparo")
"qui ci metti il comando che distrugge il vetro"

dovrebbe funzionare comnuque se posti lo script potrei modificartelo io, come vuoi. Smile
(15-05-2015, 08:05 AM)Master Gabriel Ha scritto: [ -> ]Uhm, prima di tutto dovresti postare lo script. Poi per fare in modo che si distrugga quando lo sparo lo tocca, lo sparo deve avere un collider con una tag in modo che nello script scriverai:

if(other.collider.tag == "Sparo")
"qui ci metti il comando che distrugge il vetro"

dovrebbe funzionare comnuque se posti lo script potrei modificartelo io, come vuoi. Smile

ecco il codice che ce già sull'oggetto vetro:

Codice:
using UnityEngine;
using System.Collections.Generic;

public class BreakGlass : MonoBehaviour {
    public List<GameObject> BrokenGlassGO; // The broken glass GameObject
    GameObject BrokenGlassInstance;
    public bool BreakSound=true;
    public GameObject SoundEmitter; //An object that will emit sound
    public float SoundEmitterLifetime=2.0f;
    public float ShardsLifetime=3.0f; //Lifetime of shards in seconds (0 if you don't want shards to disappear)
    public float ShardMass=0.5f; //Mass of each shard
    public Material ShardMaterial;
    
    public bool BreakByVelocity=true;
    public float BreakVelocity=2.0f;
    
    public bool BreakByImpulse=false;
    public float BreakImpulse=2.0f; // Impulse of rigidbody Impulse = Mass * Velocity.magnitude
    
    public bool BreakByClick=false;
    
    public float SlowdownCoefficient=0.6f; // Percent of speed that hitting object has after the hit
    
    
    
    /*
    / If you want to break the glass call this function ( myGlass.SendMessage("BreakIt") )
    */
    public void BreakIt(){
        BrokenGlassInstance = Instantiate(BrokenGlassGO[Random.Range(0,BrokenGlassGO.Count)], transform.position, transform.rotation) as GameObject;
        
        BrokenGlassInstance.transform.localScale = transform.lossyScale;
        
        foreach(Transform t in BrokenGlassInstance.transform){
            t.GetComponent<Renderer>().material = ShardMaterial;
            t.GetComponent<Rigidbody>().mass=ShardMass;
        }
        
        if(BreakSound) Destroy(Instantiate(SoundEmitter, transform.position, transform.rotation) as GameObject, SoundEmitterLifetime);
        
        if(ShardsLifetime>0) Destroy(BrokenGlassInstance,ShardsLifetime);
        Destroy(gameObject);
    }
    
    void OnMouseDown () {
        if(BreakByClick) BreakIt();    
    }
}

non so proprio come muovermi....
using UnityEngine;
using System.Collections.Generic;

public class BreakGlass : MonoBehaviour {
public List<GameObject> BrokenGlassGO; // The broken glass GameObject
GameObject BrokenGlassInstance;
public bool BreakSound=true;
public GameObject SoundEmitter; //An object that will emit sound
public float SoundEmitterLifetime=2.0f;
public float ShardsLifetime=3.0f; //Lifetime of shards in seconds (0 if you don't want shards to disappear)
public float ShardMass=0.5f; //Mass of each shard
public Material ShardMaterial;

public bool BreakByVelocity=true;
public float BreakVelocity=2.0f;

public bool BreakByImpulse=false;
public float BreakImpulse=2.0f; // Impulse of rigidbody Impulse = Mass * Velocity.magnitude

public bool BreakByClick=false;

public float SlowdownCoefficient=0.6f; // Percent of speed that hitting object has after the hit



/*
/ If you want to break the glass call this function ( myGlass.SendMessage("BreakIt") )
*/
public void BreakIt(){
BrokenGlassInstance = Instantiate(BrokenGlassGO[Random.Range(0,BrokenGlassGO.Count)], transform.position, transform.rotation) as GameObject;

BrokenGlassInstance.transform.localScale = transform.lossyScale;

foreach(Transform t in BrokenGlassInstance.transform){
t.GetComponent<Renderer>().material = ShardMaterial;
t.GetComponent<Rigidbody>().mass=ShardMass;
}

if(BreakSound) Destroy(Instantiate(SoundEmitter, transform.position, transform.rotation) as GameObject, SoundEmitterLifetime);

if(ShardsLifetime>0) Destroy(BrokenGlassInstance,ShardsLifetime);
Destroy(gameObject);
}

void OnCollisionEnter ()
{
if(other.collider.tag == "Sparo")
{
if(BreakByClick) BreakIt();
}
}
}


Prova questo. Devi attaccare un collider con la tag Sparo al gameobject che funge da proiettile.
Nah non funziona! mi dice: "Assets/Breakable Glass/Scripts/BreakGlass.cs(48,20): error CS0103: The name `other' does not exist in the current context"

che faccio?
Scusami ho sbagliato! Devi fare
void OnCollisionEnter (Collision other)

Così dovrebbe andare
(15-05-2015, 08:52 PM)Master Gabriel Ha scritto: [ -> ]Scusami ho sbagliato! Devi fare
void OnCollisionEnter (Collision other)

Così dovrebbe andare

Sistemato! funziona benissimo! Ora sorge il problema della distruzione!

ho questo script che raggruppa tutti i pezzi rotti sotto un'unico solo:

Codice:
function Start ()
{
    transform.DetachChildren();
    Debug.Log ("Detached");
    Destroy(gameObject);
    
}

Il problema è che la distruzione non avviene mai, ho provato a inserire una distruzione con un ritardo di 5 secondi ma niente...Sai come fare? è in JS
Lo script è attaccato al gameobject padre?
sistemato! tutto ok, ho aggiunto anche i particles! Wink
Bene! Smile Big Grin Smile