public void OnCollisionEnter(Collision collision)
        {
            // Check that the impact is forceful enough to cause damage
            if (collision.relativeVelocity.magnitude < minHitVelocity)
            {
                return;
            }

            if (collision.contacts.Length == 0)
            {
                return;
            }

            Collider other = collision.contacts[0].otherCollider;

            // Play hit effects
            HitEffects hitEffects = other.gameObject.GetComponentInParent <HitEffects>();

            if (hitEffects != null && hitEffects.effects.Count > 0)
            {
                hitEffects.PlayEffect(weaponType, collision.contacts[0].point, collision.contacts[0].normal);
            }

            // Apply impact damage to Destructible objects without rigidbodies
            Destructible destructibleObj = other.gameObject.GetComponentInParent <Destructible>();

            if (destructibleObj != null)
            {
                if (other.attachedRigidbody == null || other.attachedRigidbody.GetComponent <Destructible>() == null)
                {
                    if (collision.relativeVelocity.magnitude >= destructibleObj.ignoreCollisionsUnder)
                    {
                        destructibleObj.ProcessDestructibleCollision(collision, gameObject.GetComponent <Rigidbody>());
                        rbody.velocity = lastVelocity;
                    }
                }
            }

            // Check for Chip-Away Debris
            ChipAwayDebris chipAwayDebris = collision.contacts[0].otherCollider.gameObject.GetComponent <ChipAwayDebris>();

            if (chipAwayDebris != null)
            {
                chipAwayDebris.BreakOff(collision.relativeVelocity * -1, collision.contacts[0].point);
            }
        }
Example #2
0
        private void AddAffectedObjects(int colliderCount, ExplosiveDamage explosiveDamage, float chipAwayPercentage)
        {
            for (int i = 0; i < colliderCount; i++)
            {
                Collider col = DestructionManager.Instance.overlapColliders[i];

                // Ignore terrain colliders
                if (col is TerrainCollider)
                {
                    continue;
                }

                // Ignore self (the rocket)
                if (col == GetComponent <Collider>())
                {
                    continue;
                }

                // Check for Rigidbodies
                Rigidbody rbody = col.attachedRigidbody;
                if (rbody != null && !rbody.isKinematic && !affectedRigidbodies.Contains(rbody))
                {
                    affectedRigidbodies.Add(rbody);
                }

                // Check for Chip-Away Debris
                ChipAwayDebris chipAwayDebris = col.gameObject.GetComponent <ChipAwayDebris>();
                if (chipAwayDebris != null && !affectedChipAwayDebris.ContainsKey(chipAwayDebris))
                {
                    affectedChipAwayDebris.Add(chipAwayDebris, chipAwayPercentage);
                }

                if (chipAwayDebris != null)
                {
                    continue; // Don't process destructible components on chip-away debris.
                }
                // Check for Destructible objects
                Destructible destructible = col.gameObject.GetComponentInParent <Destructible>();
                if (destructible != null && !affectedDestructibles.ContainsKey(destructible))
                {
                    affectedDestructibles.Add(destructible, explosiveDamage);
                }
            }
        }
Example #3
0
        private void OnTriggerEnter(Collider col)
        {
            Destructible destObj = col.gameObject.GetComponentInParent <Destructible>();

            // If it has a rigidbody, apply force
            Rigidbody rbody = col.attachedRigidbody;

            if (rbody != null && !rbody.isKinematic)
            {
                rbody.AddExplosionForce(blastForce, origin, 0f, 0.5f);
            }

            // Check for Chip-Away Debris
            ChipAwayDebris chipAwayDebris = col.gameObject.GetComponent <ChipAwayDebris>();

            if (chipAwayDebris != null)
            {
                if (Random.Range(1, 100) > 50) // Do this about half the time...
                {
                    chipAwayDebris.BreakOff(blastForce, 0f, 0.5f);
                    return; //Skip the destructible check if the debris hasn't chipped away yet.
                }

                return;
            }

            // If it's a destructible object, apply damage
            if (destObj != null)
            {
                destObj.ApplyDamage(new ExplosiveDamage()
                {
                    DamageAmount   = damageAmount,
                    BlastForce     = blastForce,
                    Position       = origin,
                    Radius         = 0f,
                    UpwardModifier = 0.5f
                });
            }
        }
Example #4
0
        public void ProcessBulletHit(RaycastHit hitInfo, Vector3 bulletDirection)
        {
            HitEffects hitEffects = hitInfo.collider.gameObject.GetComponentInParent <HitEffects>();

            if (hitEffects != null && hitEffects.effects.Count > 0)
            {
                hitEffects.PlayEffect(HitBy.Bullet, hitInfo.point, hitInfo.normal);
            }

            // Apply damage if object hit was Destructible
            Destructible destObj = hitInfo.collider.gameObject.GetComponentInParent <Destructible>();

            if (destObj != null)
            {
                ImpactDamage bulletImpact = new ImpactDamage()
                {
                    DamageAmount = Instance.bulletDamage, AdditionalForce = Instance.bulletForcePerSecond, AdditionalForcePosition = hitInfo.point, AdditionalForceRadius = .5f
                };
                destObj.ApplyDamage(bulletImpact);
            }

            Vector3 force = bulletDirection * (Instance.bulletForcePerSecond / Instance.bulletForceFrequency);

            // Apply impact force to rigidbody hit
            Rigidbody rbody = hitInfo.collider.attachedRigidbody;

            if (rbody != null)
            {
                rbody.AddForceAtPosition(force, hitInfo.point, ForceMode.Impulse);
            }

            // Check for Chip-Away Debris
            ChipAwayDebris chipAwayDebris = hitInfo.collider.gameObject.GetComponent <ChipAwayDebris>();

            if (chipAwayDebris != null)
            {
                chipAwayDebris.BreakOff(force, hitInfo.point);
            }
        }