Ejemplo n.º 1
0
 // Use this for initialization
 void Start()
 {
     fireFrom    = new Vector3(0, 0.26f, 0.4f);
     readyToFire = true;
     //aimTarget = GameObject.Find("Player");
     MaxHealth = 3;
     Health    = 3;
     AIState   = SoldierAI.State.Marching;
     fireSpeed = 8;
     maxRange  = (Mathf.Pow(fireSpeed, 2) / 9.81f);
     //HealthBar = this.transform.GetChild(1).GetChild(0).GetChild(0);
 }
Ejemplo n.º 2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (aimTarget == null)
        {
            foreach (GameObject item in GameObject.FindGameObjectsWithTag("SoldierSpawner"))
            {
                if (item.GetComponent <SoldierSpawner>().Allegiance != allegiance)
                {
                    aimTarget = item;
                    break;
                }
            }
        }
        targetRange       = (aimTarget.transform.position - (this.transform.position + fireFromAdjusted)).magnitude;
        directionOfTarget = (aimTarget.transform.position - this.transform.position).normalized;
        turnQuat          = Quaternion.LookRotation(directionOfTarget) * Quaternion.Inverse(this.transform.rotation);
        GetComponent <Rigidbody>().AddTorque(turnQuat.x * turnQuat.w, turnQuat.y * turnQuat.w, turnQuat.z * turnQuat.w);
        GetComponent <Rigidbody>().AddForce(0.0f, speed * (0.5f - this.transform.position.y), 0.0f);

        if (targetRange < maxRange)
        {
            AIState = SoldierAI.State.Firing;
        }
        else
        {
            AIState = SoldierAI.State.Marching;
        }

        switch (AIState)
        {
        case SoldierAI.State.Marching:
            Vector3 movement = (aimTarget.transform.position - transform.position);
            movement = new Vector3(movement.x, 0.0f, movement.z);
            movement.Normalize();
            GetComponent <Rigidbody>().AddForce(movement * speed);
            break;

        case SoldierAI.State.Firing:
            if (readyToFire && FacingAimDirection())
            {
                FireCannon(aimTarget.transform);
            }
            break;

        case SoldierAI.State.Reloading:
            break;
        }
    }