Esempio n. 1
0
        /// <summary>
        /// Explode when the missile collides with something.
        /// </summary>
        /// <param name="hit">Information about the collision.</param>
        private void OnControllerColliderHit(ControllerColliderHit hit)
        {
            GameObject explosion = Instantiate(ExplosionPrefab);

            explosion.transform.position = gameObject.transform.position;
            ActionTimer despawnTimer = explosion.AddComponent <ActionTimer>();

            despawnTimer.Action = delegate {
                Destroy(explosion);
            };
            despawnTimer.Expiry = ExplosionExpiry;

            Collider[] damageTargets = Physics.OverlapSphere(
                transform.position, ExplosionRadius, DamageLayers);
            foreach (Collider collider in damageTargets)
            {
                GameObject   explodee     = collider.gameObject;
                FenceChecker fenceChecker = explodee.GetComponent <FenceChecker>();

                if ((fenceChecker != null) && (!fenceChecker.Fenced()))
                {
                    BuildingExploder exploder = explodee.GetComponent <BuildingExploder>();
                    if (exploder != null)
                    {
                        exploder.Explode(gameObject.transform.position);
                    }
                }
            }

            Destroy(gameObject);
        }
Esempio n. 2
0
        /// <summary>
        /// Spawn and throw a chunk with the specified volume.
        /// </summary>
        /// <param name="explosionPosition">The position of the explosion force.</param>
        /// <param name="volume">The volume of the chunk.</param>
        private void ThrowChunk(Vector3 explosionPosition, float volume)
        {
            Physics.IgnoreLayerCollision(DEBRIS_LAYER, 0);

            float dims = Mathf.Pow(volume, 1.0f / 3.0f);

            GameObject chunk   = GameObject.CreatePrimitive(PrimitiveType.Cube);
            float      force   = 5000f;
            float      spacing = 5f;

            chunk.transform.localScale = Vector3.one * dims;
            chunk.transform.position   = gameObject.transform.position
                                         + new Vector3(
                Random.Range(-spacing, spacing),
                Random.Range(0, spacing),
                Random.Range(-spacing, spacing));
            Rigidbody rigidbody = chunk.AddComponent <Rigidbody>();

            rigidbody.AddExplosionForce(force, explosionPosition, 50f, 100f);

            chunk.layer = DEBRIS_LAYER;

            ActionTimer despawnTimer = chunk.AddComponent <ActionTimer>();

            despawnTimer.Action = delegate {
                Destroy(chunk);
            };
            despawnTimer.Expiry = DEBRIS_LIFETIME;

            Renderer chunkRenderer = chunk.GetComponent <Renderer>();

            chunkRenderer.sharedMaterial = ChunkMaterial;
        }
Esempio n. 3
0
        /// <summary>
        /// Fire a missile from the player with the same orientation as the player.
        /// </summary>
        private void Fire(Vector3 targetPos)
        {
            GameObject missile = Instantiate(MissilePrefab);

            missile.transform.position = Muzzle.transform.position;
            missile.transform.LookAt(targetPos);
            ActionTimer despawnTimer = missile.AddComponent <ActionTimer>();

            despawnTimer.Action = delegate {
                Destroy(missile);
            };
            despawnTimer.Expiry = 32;

            // Put the missile on a special layer so it can be made to collide with the fencing zones.
            missile.layer = MissilePrefab.layer;

            Collider missileCollider = missile.GetComponent <Collider>();
            Collider playerCollider  = Player.GetComponent <Collider>();

            Physics.IgnoreCollision(missileCollider, playerCollider);
        }