Exemplo n.º 1
0
        /// <summary>
        /// Create a default RaycastHitInfo. subclass can override this method to create a new type of RaycastHitInfo
        /// </summary>
        /// <param name="other">The collider that this bullet hits with</param>
        /// <returns> RaycastHitInfo </returns>
        protected virtual RaycastHitEventArgs CreateHitInfo(UnityEngine.Collider other)
        {
            RaycastHitEventArgs info = new RaycastHitEventArgs(Shooter, HitType.Bullet | HitType.Raycast, other);

            info.Damage     = Damage;
            info.DamageType = DamageType;
            info.Tag        = this.tag;
            return(info);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update
        /// </summary>
        protected override void Update()
        {
            if (Global.IsGamePaused)
            {
                return;
            }

            Vector3 prePosition = transform.position;

            // update to move bullet forward
            base.Update();

            _Ray.direction = Direction;
            _Ray.origin    = prePosition;
            float distance = Vector3.Distance(transform.position, prePosition);

            if (distance > Mathf.Epsilon)
            {
                if (Physics.Raycast(_Ray, out _Hit, distance, LayerMask))
                {
                    EventManager events = _Hit.collider.GetComponent <EventManager>();
                    if (events != null)
                    {
                        RaycastHitEventArgs hitInfo = CreateHitInfo(_Hit.collider);
                        hitInfo.Normal = _Hit.normal;
                        hitInfo.Point  = _Hit.point;

                        // set distance of bullet to real value travelled by bullet
                        _Hit.distance      = TravelledDistance - (distance - _Hit.distance);
                        hitInfo.RaycastHit = _Hit;

                        events.RaiseHit(this, hitInfo);
                    }

                    OnDie(_Hit.collider);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Instantiate a bullet and throw it at correct direction and force
        /// </summary>
        /// <param name="bulletCount">Number of bullet to shoot</param>
        /// <returns> Array of spawned bullets </returns>
        protected virtual Bullet[] ShootRaycastBullets(int bulletCount)
        {
            Bullet[] bullets = new Bullet[bulletCount];

            Vector3 direction;

            if (Target != null && Target.HasValue)
            {
                direction = (Target.Value - SelectedProjectile.SpawnPosition).normalized;
            }
            else
            {
                direction = SelectedProjectile.SpawnPoint.forward;
            }

            for (int i = 0; i < bulletCount; i++)
            {
                Quaternion spreadRot       = Quaternion.Euler(UnityEngine.Random.Range(-Spread.x, Spread.x), UnityEngine.Random.Range(-Spread.y, Spread.y), 0);
                Quaternion bulletRotation  = Quaternion.LookRotation(spreadRot * direction);
                Vector3    bulletDirection = (bulletRotation * Vector3.forward).normalized;

                // spawn a bullet but inactive
                GameObject         go     = Cache.Spawn(SelectedProjectile.BulletPrefab, SelectedProjectile.SpawnPosition, bulletRotation, false) as GameObject;
                StraightLineBullet bullet = go.GetComponent <StraightLineBullet>(); // get reference to bullet

                if (bullet != null)                                                 // set bullet parameters
                {
                    bullet.Shooter   = this.gameObject;
                    bullet.Damage    = SelectedProjectile.InstantHitDamage * DamageFactor;
                    bullet.Direction = bulletDirection;
                    bullet.Range     = SelectedProjectile.Range;
                    bullet.Speed     = SelectedProjectile.InitialSpeed;
                    bullet.LayerMask = SelectedProjectile.LayerMask;

                    if (SelectedProjectile.HitAtSpawn) // if is is needed to check hit at spawn time
                    {
                        _Ray.direction = bulletDirection;
                        _Ray.origin    = SelectedProjectile.SpawnPosition;

                        if (Physics.Raycast(_Ray, out _HitInfo, SelectedProjectile.Range, SelectedProjectile.LayerMask))
                        {
                            bullet.Range = _HitInfo.distance;

                            EventManager events = _HitInfo.collider.GetComponent <EventManager>();
                            if (events != null)
                            {
                                RaycastHitEventArgs info = new RaycastHitEventArgs(bullet.Shooter, HitType.Bullet | HitType.Raycast, _HitInfo.collider);
                                info.Damage     = bullet.Damage;
                                info.DamageType = bullet.DamageType;
                                info.Tag        = this.tag;
                                info.Normal     = _HitInfo.normal;
                                info.Point      = _HitInfo.point;
                                info.RaycastHit = _HitInfo;
                                events.RaiseHit(this, info);
                            }
                        }
                    }
                }
                bullets[i] = bullet;
            }
            return(bullets);
        }