Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (objective != null)
        {
            //====================================== follow objective ======================================
            float distance = Vector3.SqrMagnitude(this.gameObject.transform.position - objective.transform.position);
            //-------------------------- Rotation --------------------------
            Vector3 desiredRotation;
            if (distance > sqInRange)
            {
                desiredRotation = objective.transform.position - (this.transform.position + destinationShift);
            }
            else
            {
                desiredRotation = objective.transform.position - this.transform.position;
            }
            desiredRotation.y      = 0;
            this.transform.forward = Vector3.RotateTowards(this.transform.forward, desiredRotation, turningSpeed * Time.deltaTime, 0);

            //-------------------------- Moovement --------------------------
            if (distance > close)
            {
                applyMoovement(new Vector2(runningSpeed, 0));
            }
            else
            {
                if (distance > sqInRange)
                {
                    applyMoovement(new Vector2(walkingSpeed, 0));
                }
                else
                {
                    applyMoovement(Vector2.zero);
                }
            }
        }
        else
        {
            //====================================== get new objective ======================================
            if (imGood)
            {
                objective = HiveMind.getClosestBadGuy(this.transform.position);
            }
            else
            {
                objective = HiveMind.getClosestGoodGuy(this.transform.position);
            }
            //get new random side
            Vector2 aux = Random.insideUnitCircle.normalized * inRange;
            destinationShift = new Vector3(aux.x, 0, aux.y);
            //stop moovement
            applyMoovement(Vector2.zero);
        }

        //Update the character position
        refresh();
    }
Example #2
0
 void Update()
 {
     if (objective != null)
     {
         float   distance        = Vector3.SqrMagnitude(moovingPart.position - objective.transform.position);
         Vector3 desiredRotation = objective.transform.position - moovingPart.position;
         moovingPart.forward = Vector3.RotateTowards(moovingPart.forward, desiredRotation, rotationSpeed * Time.deltaTime, 0);
         if ((Time.time - lastShot) > timeBetweenShots)
         {
             if (Physics.Raycast(endOfBarrel.transform.position, endOfBarrel.forward, out hit, 500))
             {
                 if (hit.collider.gameObject == objective.gameObject)
                 {
                     //shoot
                     Instantiate(projectile, endOfBarrel.position, Quaternion.Euler(endOfBarrel.eulerAngles + (Random.insideUnitSphere * acuracy)));
                     lastShot = Time.time;
                 }
             }
         }
         if (distance > shootingRange)
         {
             objective = null;
         }
     }
     else
     {
         if (imGood)
         {
             objective = HiveMind.getClosestBadGuy(this.transform.position);
         }
         else
         {
             objective = HiveMind.getClosestGoodGuy(this.transform.position);
         }
     }
 }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        if (objective != null)
        {
            if (atacking == false)
            {
                if (engaged == false)
                {
                    //====================================== follow objective ======================================
                    float distance = Vector3.SqrMagnitude(this.gameObject.transform.position - objective.transform.position);
                    //-------------------------- Rotation --------------------------
                    Vector3 desiredRotation;
                    if (distance > sqInRange)
                    {
                        desiredRotation = objective.transform.position - (this.transform.position + destinationShift);
                    }
                    else
                    {
                        desiredRotation = objective.transform.position - this.transform.position;
                    }
                    desiredRotation.y      = 0;
                    this.transform.forward = Vector3.RotateTowards(this.transform.forward, desiredRotation, turningSpeed * Time.deltaTime, 0);

                    //-------------------------- Moovement --------------------------
                    if (distance > close)
                    {
                        applyMoovement(new Vector2(runningSpeed, 0));
                    }
                    else
                    {
                        if (distance > sqInRange)
                        {
                            applyMoovement(new Vector2(walkingSpeed, 0));
                        }
                        else
                        {
                            if (distance > engage)
                            {
                                applyMoovement(Vector2.zero);
                                RaycastHit hit;
                                if (Physics.Linecast(this.transform.position, this.transform.position + this.transform.forward * inRange, out hit))
                                {
                                    if (hit.collider.gameObject == objective.gameObject)
                                    {
                                        animator.SetBool("Attacking", true);
                                        atacking = true;
                                    }
                                }
                            }
                            else
                            {
                                engaged = true;
                                animator.SetBool("Engaged", true);
                                lasteEngagement = Time.time;
                                applyMoovement(Vector2.zero);
                            }
                        }
                    }
                }
                else
                {
                    //engaged
                    this.gameObject.transform.Rotate(new Vector3(0, turningSpeed * 57.3f * Time.deltaTime, 0));                      // 57.3 is so to transform radians to degrees
                    Vector3 moovement = objective.transform.position - this.transform.position;
                    moovement.y = 0;
                    applyPush(moovement.normalized * walkingSpeed);
                    if (Time.time - lasteEngagement > engagmentTime && (objective.transform.position - this.transform.position).sqrMagnitude > engage)
                    {
                        animator.SetBool("Engaged", false);
                    }
                }
            }
            else
            {
                //attacking
                applyMoovement(Vector2.zero);
            }
        }
        else
        {
            //====================================== get new objective ======================================
            if (imGood)
            {
                objective = HiveMind.getClosestBadGuy(this.transform.position);
            }
            else
            {
                objective = HiveMind.getClosestGoodGuy(this.transform.position);
            }
            //get new random side
            Vector2 aux = Random.insideUnitCircle.normalized * inRange;
            destinationShift = new Vector3(aux.x, 0, aux.y);
            //stop moovement
            applyMoovement(Vector2.zero);
        }

        //Update the character position
        refresh();

        //Controling lasers
        if (atacking)
        {
            lasers [0].framecall();
        }
        if (engaged)
        {
            for (int i = 0; i < 3; i++)
            {
                lasers [i].framecall();
            }
        }
    }
Example #4
0
    // Update is called once per frame
    void Update()
    {
        if (objective != null)
        {
            if (stunned == false)
            {
                //====================================== follow objective ======================================
                float distance = Vector3.SqrMagnitude(objective.transform.position - this.transform.position);
                //-------------------------- Rotation --------------------------
                blades.transform.Rotate(new Vector3(0, 0, turningSpeed * Time.deltaTime));

                //-------------------------- Moovement --------------------------
                if (atacking)
                {
                    //it's atacking
                    if (distance > close)
                    {
                        //its slowing down
                        float lerpFactor = (Time.time - lastReorient) / reorientPeriod;
                        sphere.Move(Vector3.Lerp(currentSpeed, Vector3.zero, lerpFactor) * Time.deltaTime);
                    }
                    else
                    {
                        lastReorient = Time.time;
                        sphere.Move(currentSpeed * Time.deltaTime);
                    }
                    if ((Time.time - lastReorient) >= reorientPeriod)
                    {
                        atacking = false;
                    }
                }
                else
                {
                    Vector3 moovement = (objective.transform.position - this.transform.position).normalized;
                    sphere.Move(moovement * walkingSpeed * Time.deltaTime);
                    if (distance < close)
                    {
                        atacking     = true;
                        currentSpeed = moovement * runningSpeed;
                    }
                }
            }
            else
            {
                //is stunned
                float lerpFactor = (Time.time - lastStunned) / stunnedPeriod;
                sphere.Move(Vector3.Lerp(currentSpeed, Vector3.zero, lerpFactor) * Time.deltaTime);
                blades.transform.Rotate(new Vector3(0, 0, Mathf.Lerp(0, turningSpeed, lerpFactor) * Time.deltaTime));
                if (Time.time - lastStunned > stunnedPeriod)
                {
                    stunned = false;
                }
            }
        }
        else
        {
            //====================================== get new objective ======================================
            if (imGood)
            {
                objective = HiveMind.getClosestBadGuy(this.transform.position);
            }
            else
            {
                objective = HiveMind.getClosestGoodGuy(this.transform.position);
            }
        }
    }