Exemple #1
0
    private void OnEnable()
    {
        Enemies.Clear();
        Enemies.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
        closestGameObject = StaticMethods.ClosestTargetToPlayerWithinRange(Enemies, player.transform.position);
        Invoke("HideProjectile", lifetime);

        Collider[] initialCollisions = Physics.OverlapSphere(transform.position, 1f, collisionMask);
        if (initialCollisions.Length > 0)
        {
            OnHitObject(initialCollisions[0], transform.position); // first thing it collides with
        }
    }
Exemple #2
0
    protected virtual void Update()
    {
        currentPosition = GetComponent <Transform>().position;
        Enemies.Clear();         // To stop null pointer exception when an enemy dies. Expensive???

        if (target == Target.enemyClosestToPlayer)
        {
            Enemies.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
            closestGameObject = StaticMethods.ClosestTargetToPlayerWithinRange(Enemies, player.transform.position);
        }

        if (target == Target.enemyClosestToShip && ship != null)
        {
            Enemies.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
            closestGameObject = StaticMethods.ClosestTargetToShipWithinRange(Enemies, transform.position, ship.transform.position, shootRange);
        }

        if (target == Target.shipsTarget && ship != null)
        {
            Enemies.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
            closestGameObject = StaticMethods.ClosestTargetToShipWithinRange(Enemies, transform.position, transform.position, shootRange);
        }

        if (closestGameObject != null)
        {
            float dist = Vector3.Distance(closestGameObject.transform.position, transform.position);
            if (dist <= shootRange && !weaponDisabled)
            {
                Vector3    relativePos = closestGameObject.transform.position - transform.position;
                Quaternion rotation    = Quaternion.LookRotation(relativePos);
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
                weaponController.Shoot();
            }
            else
            {
                Vector3    relativePos = closestGameObject.transform.position - transform.position;
                Quaternion rotation    = Quaternion.LookRotation(relativePos);
                transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
            }
        }
        else
        {
            if (currentPosition == lastPosition)              // if the object that this is on is not moving
            {
                if (target == Target.enemyClosestToShip)
                {
                    transform.rotation = GameObject.FindGameObjectWithTag("Player Rotation").GetComponent <Transform>().rotation;
                }
                else
                {
                    if (ship != null && target != Target.shipsTarget)
                    {
                        transform.rotation = GameObject.FindGameObjectWithTag("Player Rotation").GetComponent <Transform>().rotation;
                    }
                    else
                    {
                        Vector3    relativePos = new Vector3(Camera.main.transform.position.x, 0f, Camera.main.transform.position.z) - transform.position;
                        Quaternion rotation    = Quaternion.LookRotation(relativePos);
                        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
                    }
                }
            }
            else
            {
                if (target != Target.shipsTarget)
                {
                    transform.rotation = GameObject.FindGameObjectWithTag("Player Rotation").GetComponent <Transform>().rotation;
                }
                else
                {
                    Vector3    relativePos = new Vector3(Camera.main.transform.position.x, 0f, Camera.main.transform.position.z) - transform.position;
                    Quaternion rotation    = Quaternion.LookRotation(relativePos);
                    transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
                }
            }
            lastPosition = currentPosition;
        }
    }