Jump to content
Unity Insider Forum

Destroy zerstört alle Clone


Shalafi

Recommended Posts

Hi,

Ich hab ein Skript geschrieben, dass Granaten wirft.


Das Problem ist, dass wenn ich mehrere Granaten werfe, und die erste sich nach der Explosion selbst löscht (Destroy(this.gameObject);), werden alle Granaten gelöscht, auch die, die noch nicht explodiert sind.

Hier mein Skript:

public class ThrowGranade : MonoBehaviour
    {
        public GameObject GrenadePrefab;
        private Transform MyTransform;
        private GameObject GrenadeObj;
        private Transform CameraTransform;
        public float ThrowForce = 5f;

        // Use this for initialization
        void Start()
        {
            this.MyTransform = this.transform;
            this.CameraTransform = this.transform.Find("Body/PlayerCamera").transform;
        }

        // Update is called once per frame
        void Update()
        {
            if (Input.GetButtonDown("ThrowGrenade"))
            {
                this.InstantiateGrenade();
            }
            else if (Input.GetButtonUp("ThrowGrenade"))
            {
                this.ThrowGrenade();
            }
        }

        private void InstantiateGrenade()
        {
            if (this.GrenadePrefab != null)
            {
                this.GrenadeObj = (GameObject)Instantiate(this.GrenadePrefab, this.CameraTransform.TransformPoint(.45f,-.25f,.7f), this.CameraTransform.rotation);
                this.GrenadeObj.GetComponent<Rigidbody>().isKinematic = true;
                this.GrenadeObj.transform.SetParent(this.CameraTransform, true);
            }
        }

        private void ThrowGrenade()
        {
            if (this.GrenadeObj != null)
            {
                this.GrenadeObj.GetComponent<Grenade>().Throw(Time.time);
                this.GrenadeObj.transform.SetParent(null, true);
                this.GrenadeObj.GetComponent<Rigidbody>().useGravity = true;
                this.GrenadeObj.GetComponent<Rigidbody>().isKinematic = false;
                this.GrenadeObj.GetComponent<Rigidbody>().AddForce(this.CameraTransform.forward * this.ThrowForce, ForceMode.Impulse);
                this.GrenadeObj.GetComponent<Rigidbody>().AddForce(this.MyTransform.up * this.ThrowForce, ForceMode.Impulse);
            }
        }
    }

Kann man das irgendwie verhindern?

Google konnte mir da auch nicht richtig weiterhelfen.

 



Gruß

Link zu diesem Kommentar
Auf anderen Seiten teilen

Hi,

hier ist es:

public class Grenade : MonoBehaviour
    {
        public float Explosiontime { get; set; }
        public float Countdown = 5f;
        public float BlastRadius = 5f;

        public float ExplosionPower = 100f;
        public float MaxDmg = 100f;

        void Start()
        {
            this.Explosiontime = -1;
        }

        void Update()
        {
            if (this.Explosiontime > 0 && Time.time >= this.Explosiontime)
            {
                this.Explode();
            }
        }

        //Countdown beginnt
        public void Throw(float throwtime)
        {
            this.Explosiontime = throwtime + this.Countdown;
        }

        //Explosion
        private void Explode()
        {
            Explosion.Explode(this.transform.position, this.BlastRadius, this.ExplosionPower, this.MaxDmg);
            Destroy(this.gameObject);
        }
    }

Und das Skript für die Explosion auch noch:
 

public static class Explosion
    {
        public static void Explode(Vector3 explosion_position, float BlastRadius, float ExplosionPower, float MaxDMG)
        {
            Collider[] hitColliders;
            hitColliders = Physics.OverlapSphere(explosion_position, BlastRadius);
            foreach (Collider hitcol in hitColliders)
            {
                if (hitcol.GetComponent<Rigidbody>() != null || hitcol.GetComponent<GaW.Player.PlayerStats>() != null)
                {
                    //Gucken, das nix im Weg 
                    RaycastHit hit;
                    bool wallhit = false;
                    if (Physics.Raycast(explosion_position, hitcol.transform.position - explosion_position, out hit, BlastRadius))
                    {
                        if (hit.transform.GetComponent<Rigidbody>() == null && hit.collider != hitcol && hit.transform.tag != "Player")
                        {
                            wallhit = true;
                        }
                    }

                    if (wallhit == false)
                    {
                        if (hitcol.GetComponent<Rigidbody>() != null)
                        {
                            hitcol.GetComponent<Rigidbody>().AddExplosionForce(ExplosionPower, explosion_position, BlastRadius, 1, ForceMode.Impulse);
                        }
                        
                        if (hitcol.GetComponent<GaW.Player.PlayerStats>() != null)
                        {
                            Vector3 closespoint = hitcol.ClosestPoint(explosion_position);
                            float dmg = MaxDMG * (1 - (Vector3.Distance(explosion_position, closespoint) / BlastRadius));
                            hitcol.GetComponent<GaW.Player.PlayerStats>().Damage(dmg);
                        }
                    }
                }
            }
        }
    }

 

Gruß

Link zu diesem Kommentar
Auf anderen Seiten teilen

Hmm... du hast nur einen Destroy-Aufruf, keine statischen Variablen... ich sehe irgendwie nichts, was dieses Problem auslösen könnte. Kann es sein, dass deine Granaten vielleicht gar nicht gelöscht werden, sondern durch AddExplosionForce einfach nur extrem schnell weggeschleudert werden?

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...