Esempio n. 1
0
    void Update()
    {
        if (Element != null)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                GameObject newObject = GameObject.Instantiate(Element) as GameObject;
                newObject.transform.position             = this.transform.position;
                newObject.transform.localScale           = new Vector3(Scale, Scale, Scale);
                newObject.rigidbody.mass                 = Mass;
                newObject.rigidbody.solverIterationCount = 255;
                newObject.rigidbody.AddForce(this.transform.forward * InitialSpeed, ForceMode.VelocityChange);

                DieTimer dieTimer = newObject.AddComponent <DieTimer>() as DieTimer;
                dieTimer.SecondsToDie = Life;
            }
        }

        if (Input.GetMouseButton(0) && Input.GetMouseButtonDown(0) == false)
        {
            this.transform.Rotate(-(Input.mousePosition.y - m_v3MousePosition.y) * MouseSpeed, 0.0f, 0.0f);
            this.transform.RotateAround(this.transform.position, Vector3.up, (Input.mousePosition.x - m_v3MousePosition.x) * MouseSpeed);
        }

        m_v3MousePosition = Input.mousePosition;
    }
Esempio n. 2
0
    public void DetachFromObject(bool bCheckStructureIntegrity = true)
    {
        if (IsDestructibleChunk() && IsDetachedChunk == false && GetComponent <Rigidbody>())
        {
            m_bNonSupportedChunkStored = IsNonSupportedChunk;

            transform.parent = null;
            GetComponent <Rigidbody>().isKinematic = false;
            GetComponent <Collider>().isTrigger    = false;
            IsDetachedChunk     = true;
            IsNonSupportedChunk = true;

            RemoveConnectionInfo();

            if (FracturedObjectSource)
            {
                FracturedObjectSource.NotifyChunkDetach(this);

                if (bCheckStructureIntegrity)
                {
                    // Check if we created isolated chunks in the air, not connected to any support chunks
                    FracturedObjectSource.CheckDetachNonSupportedChunks();
                }
            }

            // Check if we need to add a destruction timer
            if (DontDeleteAfterBroken == false && FracturedObjectSource != null)
            {
                DieTimer dieTimer = gameObject.AddComponent <DieTimer>();
                dieTimer.SecondsToDie = UnityEngine.Random.Range(FracturedObjectSource.EventDetachedMinLifeTime, FracturedObjectSource.EventDetachedMaxLifeTime);
            }
        }
    }
    public void Explode(Vector3 v3ExplosionPosition, float fExplosionForce, float fRadius, bool bPlayExplosionSound, bool bInstanceExplosionPrefabs, bool bAlsoExplodeFree, bool bCheckStructureIntegrityAfter)
    {
        // Explodes only chunks in a given radius

        List <FracturedChunk> listChunksAffected = new List <FracturedChunk>();

        // Play sound

        if (EventExplosionSound && bPlayExplosionSound)
        {
            AudioSource.PlayClipAtPoint(EventExplosionSound, v3ExplosionPosition);
        }

        // Explode chunks in range

        foreach (FracturedChunk chunk in GetDestructibleChunksInRadius(v3ExplosionPosition, fRadius, bAlsoExplodeFree))
        {
            if (chunk)
            {
                chunk.DetachFromObject(false);
                chunk.GetComponent <Rigidbody>().AddExplosionForce(fExplosionForce, v3ExplosionPosition, 0.0f, 0.0f);
                listChunksAffected.Add(chunk);
            }
        }

        // Instance prefabs on random positions

        if (bInstanceExplosionPrefabs && EventExplosionPrefabsArray.Length > 0 && EventExplosionPrefabsInstanceCount > 0 && listChunksAffected.Count > 0)
        {
            for (int i = 0; i < Mathf.Max(EventExplosionPrefabsInstanceCount, listChunksAffected.Count); i++)
            {
                FracturedObject.PrefabInfo prefab = EventExplosionPrefabsArray[UnityEngine.Random.Range(0, EventExplosionPrefabsArray.Length)];

                if (prefab != null)
                {
                    GameObject newGameObject = Instantiate(prefab.GameObject, listChunksAffected[UnityEngine.Random.Range(0, listChunksAffected.Count)].transform.position, prefab.GameObject.transform.rotation) as GameObject;

                    if (Mathf.Approximately(prefab.MinLifeTime, 0.0f) == false || Mathf.Approximately(prefab.MaxLifeTime, 0.0f) == false)
                    {
                        DieTimer timer = newGameObject.AddComponent <DieTimer>();
                        timer.SecondsToDie = UnityEngine.Random.Range(prefab.MinLifeTime, prefab.MaxLifeTime);
                    }
                }
            }
        }

        if (bCheckStructureIntegrityAfter)
        {
            CheckDetachNonSupportedChunks();
        }
    }
    public void NotifyImpact(Vector3 v3Position)
    {
        if (EventImpactSound != null)
        {
            AudioSource.PlayClipAtPoint(EventImpactSound, v3Position);
        }

        if (EventImpactPrefabsArray != null)
        {
            if (EventImpactPrefabsArray.Length > 0)
            {
                PrefabInfo prefab        = EventImpactPrefabsArray[UnityEngine.Random.Range(0, EventImpactPrefabsArray.Length)];
                GameObject newGameObject = Instantiate(prefab.GameObject, v3Position, prefab.GameObject.transform.rotation) as GameObject;

                if (Mathf.Approximately(prefab.MinLifeTime, 0.0f) == false || Mathf.Approximately(prefab.MaxLifeTime, 0.0f) == false)
                {
                    DieTimer timer = newGameObject.AddComponent <DieTimer>();
                    timer.SecondsToDie = UnityEngine.Random.Range(prefab.MinLifeTime, prefab.MaxLifeTime);
                }
            }
        }
    }
Esempio n. 5
0
    public void NotifyDetachChunkCollision(Vector3 v3Position, bool bIsMain)
    {
        // This one will be called internally from FracturedChunk from within a user-called Impact()

        if (EventDetachSound != null && bIsMain)
        {
            AudioSource.PlayClipAtPoint(EventDetachSound, v3Position);
        }

        if (EventDetachPrefabsArray != null && bIsMain)
        {
            if (EventDetachPrefabsArray.Length > 0)
            {
                PrefabInfo prefab        = EventDetachPrefabsArray[UnityEngine.Random.Range(0, EventDetachPrefabsArray.Length)];
                GameObject newGameObject = Instantiate(prefab.GameObject, v3Position, prefab.GameObject.transform.rotation) as GameObject;

                if (Mathf.Approximately(prefab.MinLifeTime, 0.0f) == false || Mathf.Approximately(prefab.MaxLifeTime, 0.0f) == false)
                {
                    DieTimer timer = newGameObject.AddComponent <DieTimer>();
                    timer.SecondsToDie = UnityEngine.Random.Range(prefab.MinLifeTime, prefab.MaxLifeTime);
                }
            }
        }
    }
    public void NotifyFreeChunkCollision(FracturedChunk.CollisionInfo collisionInfo)
    {
        if (EventDetachedCollisionCallGameObject != null && EventDetachedCollisionCallMethod.Length > 0)
        {
            EventDetachedCollisionCallGameObject.SendMessage(EventDetachedCollisionCallMethod, collisionInfo);
        }

        if (collisionInfo.bCancelCollisionEvent == false)
        {
            if (EventDetachedSoundArray.Length > 0)
            {
                int nFreeSound = -1;

                for (int nSound = 0; nSound < m_afFreeChunkSoundTimers.Length; nSound++)
                {
                    if (m_afFreeChunkSoundTimers[nSound] <= 0.0f)
                    {
                        nFreeSound = nSound;
                        break;
                    }
                }

                if (nFreeSound != -1)
                {
                    AudioClip clip = EventDetachedSoundArray[UnityEngine.Random.Range(0, EventDetachedSoundArray.Length)];

                    if (clip != null)
                    {
                        AudioSource.PlayClipAtPoint(clip, collisionInfo.collisionPoint);
                    }

                    m_afFreeChunkSoundTimers[nFreeSound] = clip.length;
                }
            }

            if (EventDetachedPrefabsArray.Length > 0)
            {
                FracturedObject.PrefabInfo prefab = EventDetachedPrefabsArray[UnityEngine.Random.Range(0, EventDetachedPrefabsArray.Length)];

                if (prefab != null)
                {
                    int nFreePrefab = -1;

                    for (int nPrefab = 0; nPrefab < m_afFreeChunkPrefabTimers.Length; nPrefab++)
                    {
                        if (m_afFreeChunkPrefabTimers[nPrefab] <= 0.0f)
                        {
                            nFreePrefab = nPrefab;
                            break;
                        }
                    }

                    if (nFreePrefab != -1)
                    {
                        GameObject newGameObject = Instantiate(prefab.GameObject, collisionInfo.collisionPoint, prefab.GameObject.transform.rotation) as GameObject;

                        if (Mathf.Approximately(prefab.MinLifeTime, 0.0f) == false || Mathf.Approximately(prefab.MaxLifeTime, 0.0f) == false)
                        {
                            DieTimer timer = newGameObject.AddComponent <DieTimer>();
                            timer.SecondsToDie = UnityEngine.Random.Range(prefab.MinLifeTime, prefab.MaxLifeTime);

                            m_afFreeChunkPrefabTimers[nFreePrefab] = timer.SecondsToDie;
                        }
                        else
                        {
                            m_afFreeChunkPrefabTimers[nFreePrefab] = float.MaxValue;
                        }
                    }
                }
            }
        }
    }
    public void Explode(Vector3 v3ExplosionPosition, float fExplosionForce)
    {
        // Explodes all chunks

        if (m_bExploded == true)
        {
            return;
        }

        // Play sound

        if (EventExplosionSound)
        {
            AudioSource.PlayClipAtPoint(EventExplosionSound, v3ExplosionPosition);
        }

        // Instance prefabs on random positions

        if (EventExplosionPrefabsArray.Length > 0 && EventExplosionPrefabsInstanceCount > 0 && ListFracturedChunks.Count > 0)
        {
            for (int i = 0; i < EventExplosionPrefabsInstanceCount; i++)
            {
                FracturedObject.PrefabInfo prefab = EventExplosionPrefabsArray[UnityEngine.Random.Range(0, EventExplosionPrefabsArray.Length)];

                if (prefab != null)
                {
                    FracturedChunk chunkRandom = null;

                    while (chunkRandom == null)
                    {
                        chunkRandom = ListFracturedChunks[UnityEngine.Random.Range(0, ListFracturedChunks.Count)];
                    }

                    GameObject newGameObject = Instantiate(prefab.GameObject, chunkRandom.transform.position, prefab.GameObject.transform.rotation) as GameObject;

                    if (Mathf.Approximately(prefab.MinLifeTime, 0.0f) == false || Mathf.Approximately(prefab.MaxLifeTime, 0.0f) == false)
                    {
                        DieTimer timer = newGameObject.AddComponent <DieTimer>();
                        timer.SecondsToDie = UnityEngine.Random.Range(prefab.MinLifeTime, prefab.MaxLifeTime);
                    }
                }
            }
        }

        // Explode chunks

        foreach (FracturedChunk chunk in ListFracturedChunks)
        {
            if (chunk)
            {
                chunk.ListAdjacentChunks.Clear();

                if (chunk.IsDestructibleChunk() && chunk.IsDetachedChunk == false)
                {
                    chunk.DetachFromObject(false);
                    chunk.GetComponent <Rigidbody>().AddExplosionForce(fExplosionForce, v3ExplosionPosition, 0.0f, 0.0f);
                }
            }
        }

        m_bExploded = true;
    }
Esempio n. 8
0
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            ShootMode = ShootMode == Mode.ExplodeRaycast ? Mode.ShootObjects : Mode.ExplodeRaycast;
        }

        if (ObjectToShoot != null && ShootMode == Mode.ShootObjects)
        {
            // Shoot objects

            if (Weapon)
            {
                Weapon.GetComponent <Renderer>().enabled = false;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                GameObject newObject = GameObject.Instantiate(ObjectToShoot) as GameObject;
                newObject.transform.position                          = this.transform.position;
                newObject.transform.localScale                        = new Vector3(ObjectScale, ObjectScale, ObjectScale);
                newObject.GetComponent <Rigidbody>().mass             = ObjectMass;
                newObject.GetComponent <Rigidbody>().solverIterations = 255;
                newObject.GetComponent <Rigidbody>().AddForce(this.transform.forward * InitialObjectSpeed, ForceMode.VelocityChange);

                DieTimer dieTimer = newObject.AddComponent <DieTimer>() as DieTimer;
                dieTimer.SecondsToDie = ObjectLife;
            }
        }

        if (ShootMode == Mode.ExplodeRaycast)
        {
            // Raycast

            if (Weapon)
            {
                Weapon.GetComponent <Renderer>().enabled = true;
            }

            bool bShot = Input.GetKeyDown(KeyCode.Space);

            if (bShot)
            {
                m_fRecoilTimer = RecoilDuration;
                if (AudioWeaponShot)
                {
                    AudioSource.PlayClipAtPoint(AudioWeaponShot, transform.position, WeaponShotVolume);
                }
            }

            m_bRaycastFound = false;

            RaycastHit hitInfo;

            FracturedChunk chunkRaycast = FracturedChunk.ChunkRaycast(transform.position, transform.forward, out hitInfo);

            if (chunkRaycast)
            {
                m_bRaycastFound = true;

                if (bShot)
                {
                    // Hit it!
                    chunkRaycast.Impact(hitInfo.point, ExplosionForce, ExplosionRadius, true);
                }
            }
        }

        // Update recoil

        if (m_fRecoilTimer > 0.0f)
        {
            if (Weapon)
            {
                // Some rudimentary recoil animation here
                Weapon.transform.localPosition = m_v3InitialWeaponPos + new Vector3(0.0f, 0.0f, (-m_fRecoilTimer / RecoilDuration) * RecoilIntensity);
                Weapon.transform.localRotation = m_qInitialWeaponRot * Quaternion.Euler(new Vector3((m_fRecoilTimer / RecoilDuration) * 360.0f * RecoilIntensity * 0.1f, 0.0f, 0.0f));
            }

            m_fRecoilTimer -= Time.deltaTime;
        }
        else
        {
            if (Weapon)
            {
                Weapon.transform.localPosition = m_v3InitialWeaponPos;
                Weapon.transform.localRotation = m_qInitialWeaponRot;
            }
        }

        // Mouse-aim

        if (Input.GetMouseButton(0) && Input.GetMouseButtonDown(0) == false)
        {
            this.transform.Rotate(-(Input.mousePosition.y - m_v3MousePosition.y) * MouseSpeed, 0.0f, 0.0f);
            this.transform.RotateAround(this.transform.position, Vector3.up, (Input.mousePosition.x - m_v3MousePosition.x) * MouseSpeed);
        }

        m_v3MousePosition = Input.mousePosition;
    }