Esempio n. 1
0
        private void ShootTrajectoryCast(CharacterShooter shooter, float deviation, CharacterShooter.ShotType shotType)
        {
            TrajectoryRenderer.TrajectoryResult result = this.GetTrajectoryResult(shooter);

            if (result.hit.collider)
            {
                Vector3 pointA = result.hit.point;
                Vector3 pointB = result.hit.point;

                if (result.points.Length > 2)
                {
                    pointA = result.points[result.count - 2];
                    pointB = result.points[result.count - 1];
                }

                Vector3    direction = (pointA - pointB).normalized;
                Quaternion rotation  = Quaternion.FromToRotation(Vector3.forward, direction);
                GameObject other     = result.hit.collider.gameObject;


                this.ExecuteShootActions(
                    other,
                    result.hit.point,
                    rotation
                    );

                IgniterOnReceiveShot[] igniters = other.GetComponentsInChildren <IgniterOnReceiveShot>();
                foreach (IgniterOnReceiveShot igniter in igniters)
                {
                    if (igniter)
                    {
                        igniter.OnRecevieShot(shooter, shotType);
                    }
                }

                if (!Mathf.Approximately(this.pushForce, 0f) && result.hit.rigidbody && direction != Vector3.zero)
                {
                    result.hit.rigidbody.AddForceAtPosition(
                        -direction * this.pushForce,
                        result.hit.point,
                        ForceMode.Impulse
                        );
                }

                if (this.prefabImpactEffect)
                {
                    GameObject impact = PoolManager.Instance.Pick(this.prefabImpactEffect);
                    impact.transform.SetPositionAndRotation(result.hit.point, rotation);
                }

                this.shootTrail.position1 = shooter.muzzle.GetPosition();
                this.shootTrail.position2 = result.hit.point;
            }

            if (this.shootTrail.useShootingTrail)
            {
                ShootingTrailRenderer.Create(this.shootTrail, result);
            }
        }
        // PUBLIC STATIC METHODS: ---------------------------------------------------------

        public static ShootingTrailRenderer Create(ShootingTrail data)
        {
            GameObject            instance = new GameObject("ShootingTrailRenderer");
            ShootingTrailRenderer trail    = instance.AddComponent <ShootingTrailRenderer>();

            trail.SetupShootingTrail(data);
            return(trail);
        }
        public static ShootingTrailRenderer Create(
            ShootingTrail data,
            TrajectoryRenderer.TrajectoryResult trajectory)
        {
            GameObject            instance = new GameObject("ShootingTrailRenderer");
            ShootingTrailRenderer trail    = instance.AddComponent <ShootingTrailRenderer>();

            trail.SetupShootingTrail(data);

            trail.lineRenderer.positionCount = trajectory.count;
            trail.lineRenderer.SetPositions(trajectory.points);

            trail.retract = (trajectory.count == 2);
            if (trail.retract)
            {
                trail.positionA = trajectory.points[0];
                trail.positionB = trajectory.points[1];
            }

            return(trail);
        }
Esempio n. 4
0
        private void ShootGenericCast(CharacterShooter shooter, float deviation,
                                      CharacterShooter.ShotType shotType, CastType castType)
        {
            ShootData shootData = this.GetShootData(shooter);

            Vector3 shootPositionRaycast = shootData.originRaycast;
            Vector3 shootPositionWeapon  = shootData.originWeapon;
            Vector3 shootPositionTarget  = shootData.destination;

            shootPositionTarget += this.CalculateError(
                shooter.gameObject,
                shootPositionRaycast,
                shootPositionTarget,
                deviation
                );

            Vector3 direction = (shootPositionTarget - shootPositionRaycast).normalized;

            int hitCounter = 0;

            switch ((((int)castType) & 2) >> 1)
            {
            case 0:     // Raycast
                hitCounter = Physics.RaycastNonAlloc(
                    shootPositionRaycast, direction, this.bufferCastHits,
                    this.distance, this.layerMask, this.triggersMask
                    );
                break;

            case 1:     // SphereCast
                hitCounter = Physics.SphereCastNonAlloc(
                    shootPositionRaycast, this.radius, direction, this.bufferCastHits,
                    this.distance, this.layerMask, this.triggersMask
                    );
                break;
            }

            int maxCount = Mathf.Min(hitCounter, this.bufferCastHits.Length);

            Array.Sort(this.bufferCastHits, 0, maxCount, SHOT_COMPARE);

            for (int i = 0; hitCounter > 0 && i < maxCount; ++i)
            {
                Vector3 point = this.bufferCastHits[i].point;
                if (this.bufferCastHits[i].distance <= 0.01f && point == Vector3.zero)
                {
                    // special case in which sphere sweep overlaps the initial value:
                    point = shootPositionWeapon + (direction.normalized * 0.1f);
                }

                if (this.bufferCastHits[i].collider.gameObject.Equals(shooter.gameObject))
                {
                    continue;
                }

                Vector3    rotationDirection = (shootPositionWeapon - point).normalized;
                Quaternion rotation          = Quaternion.FromToRotation(Vector3.forward, rotationDirection);

                GameObject other = this.bufferCastHits[i].collider.gameObject;
                this.ExecuteShootActions(other, point, rotation);

                IgniterOnReceiveShot[] igniters = other.GetComponentsInChildren <IgniterOnReceiveShot>();
                foreach (IgniterOnReceiveShot igniter in igniters)
                {
                    if (igniter)
                    {
                        igniter.OnRecevieShot(shooter, shotType);
                    }
                }

                if (!Mathf.Approximately(this.pushForce, 0f) && this.bufferCastHits[i].rigidbody)
                {
                    this.bufferCastHits[i].rigidbody.AddForceAtPosition(
                        -rotationDirection * this.pushForce,
                        point,
                        ForceMode.Impulse
                        );
                }

                shootPositionTarget = point;

                if (this.prefabImpactEffect)
                {
                    GameObject impact = PoolManager.Instance.Pick(this.prefabImpactEffect);
                    impact.transform.SetPositionAndRotation(shootPositionTarget, rotation);
                }

                if ((((int)castType) & 1) == 0)
                {
                    break;
                }
            }

            if (this.shootTrail.useShootingTrail)
            {
                this.shootTrail.position1 = shootPositionWeapon;
                this.shootTrail.position2 = shootPositionTarget;
                ShootingTrailRenderer.Create(this.shootTrail);
            }
        }