Example #1
0
        public override void Fire(Ray fireRay)
        {
            Debug.DrawRay(fireRay.origin, fireRay.direction * 100, Color.red, 10);

            if (Physics.Raycast(fireRay, out var hit, float.MaxValue, _enemyMask))
            {
                _explosionFactory.Create(hit.point, 0.1f);
                _weaponSystem.ApplyDamage(hit.collider, Damage);
            }
        }
Example #2
0
        public override void Fire(Ray fireRay)
        {
            Debug.DrawRay(fireRay.origin, fireRay.direction * 100, Color.red, 10);

            //hit floor, wall, or enemy
            if (!Physics.Raycast(fireRay, out var floorHit, float.MaxValue))
            {
                return;
            }

            var range          = ExplosionRange;
            var radius         = range * 0.5f;
            var rocketHitPoint = floorHit.point;

            _explosionFactory.Create(rocketHitPoint, range);

            //check if hitted enemy in range
            var hitsCount = Physics.OverlapSphereNonAlloc(rocketHitPoint, radius, _hits);

            if (hitsCount > 0)
            {
                //Debug.DrawRay(rocketHitPoint, Vector3.up * 5, Color.yellow, 10);

                for (int i = 0; i < hitsCount; i++)
                {
                    var hit      = _hits[i];
                    var hitPoint = hit.bounds.center;

                    var distance = Vector3.Distance(hitPoint, rocketHitPoint);
                    var damage   = Mathf.RoundToInt(Mathf.Lerp(MaxDamage, MinDamage, distance / radius));
                    _weaponSystem.ApplyDamage(hit, damage);

                    if (hit.attachedRigidbody != null)
                    {
                        hit.attachedRigidbody.AddExplosionForce(damage * 50, rocketHitPoint, radius);
                    }

                    //Debug.DrawRay(hitPoint, Vector3.up * 2, Color.magenta, 10);
                    //UnityEngine.Debug.Log($"dist {distance} / range {radius} : {damage}");
                    //Debug.DrawLine(rocketHitPoint, hitPoint, Color.green, 10);
                }
            }
        }