Exemple #1
0
        /// <summary>
        /// Shoots with a delay delay.
        /// </summary>
        /// <param name='delay'>
        /// Delay.
        /// </param>
        internal void Shoot(float delay)
        {
            Transform newShot = null;

            isShooting = true;

            if (shot)
            {
                // If the weapon has muzzles, go through all of them and create a shot in each
                if (muzzle.Length > 0)
                {
                    foreach (Transform index in muzzle)
                    {
                        if (index)
                        {
                            // Create a new shot object
                            newShot = Instantiate(shot, index.position, index.rotation) as Transform;
                            Shot actualShot = newShot.GetComponent <Shot>();

                            // If the shot has a Shot script, set its valid target and target in range from this script
                            if (actualShot)
                            {
                                actualShot.validTargets = validTargets;

                                if (actualShot.homing)
                                {
                                    if (targetInRange)
                                    {
                                        actualShot.homingTarget = targetInRange;
                                    }
                                }
                            }
                        }
                    }
                }
                else                 // If there are no muzzles, create shots in the center of this transform
                {
                    // Create a new shot object
                    newShot = Instantiate(shot, transform.position, transform.rotation) as Transform;
                    Shot actualShot = newShot.GetComponent <Shot>();

                    // If the shot has a Shot script, set its valid target and target in range from this script
                    if (actualShot)
                    {
                        actualShot.validTargets = validTargets;

                        if (actualShot.homing)
                        {
                            if (targetInRange)
                            {
                                actualShot.homingTarget = targetInRange;
                            }
                        }
                    }
                }
            }

            newShot.gameObject.SetActive(false);

            StartCoroutine(DelayForSeconds(newShot, delay));

            isShooting = false;
        }
Exemple #2
0
        /// <summary>
        /// Start is only called once in the lifetime of the behaviour.
        /// The difference between Awake and Start is that Start is only called if the script instance is enabled.
        /// This allows you to delay any initialization code, until it is really needed.
        /// Awake is always called before any Start functions.
        /// This allows you to order initialization of scripts
        /// </summary>
        public void Start()
        {
            // Remove the object after a few seconds
            if (removeAfter > 0)
            {
                Destroy(gameObject, removeAfter);
            }

            // Create a shot (muzzle) effect
            if (effect != null)
            {
                Instantiate(effect, transform.position, transform.rotation);
            }

            // If this is a homing shot, make it look for the nearest target, and then rotate towards it
            if (homing)
            {
                if (homingTarget != null)
                {
                    // Look directly at the enemy
                    if (lookAtEnemy == true)
                    {
                        transform.LookAt(homingTarget);
                    }

                    LineRenderer lr = GetComponent <LineRenderer>();

                    // If this shot has a line renderer, stretch it to the target. It's an instant hit.
                    if (lr)
                    {
                        // Stretch the line between this transform and the target
                        lr.SetPosition(1, new Vector3(0, 0, Vector3.Distance(transform.position, homingTarget.position)));

                        Enemy           enem      = homingTarget.GetComponent <Enemy>();
                        ChangeAttribute chgAttrib = homingTarget.GetComponent <ChangeAttribute>();

                        // Remove from the target enemy's health
                        if (enem)
                        {
                            enem.health -= (int)damage;
                        }

                        // If the target has a ChangeAttribute script, remove from its health counter too
                        if (chgAttrib)
                        {
                            chgAttrib.healthLossCount += damage;
                        }

                        // If there is a hit effect, create it at the hit position
                        if (hitEffect)
                        {
                            Transform newShot    = Instantiate(hitEffect, homingTarget.position, Quaternion.identity) as Transform;
                            Shot      actualShot = newShot.GetComponent <Shot>();

                            if (actualShot)
                            {
                                actualShot.validTargets = validTargets;
                            }
                        }

                        // Destroy this script
                        Destroy(this);
                    }
                }
            }
        }