Beispiel #1
0
    IEnumerator Move()
    {
        while (NextState == null)
        {
            AgentChecks();

            if (Target == null)
            {
                continue;                 //If AI has no target then skip.
            }
            float Dir = AgentFunctions.AngleDir(transform.up, (transform.position - TargetLocation), transform.forward);


            //Because I am using torque and not calculating a direction force, everything is simplified.
            Body.AddForce((transform.forward * TankSpeed) * Time.deltaTime);
            Body.AddTorque((transform.up * TurnSpeed * Dir) * Time.deltaTime);

            //Finds the turret rotation ignores the y rotation.
            Quaternion TargetRotation = AgentFunctions.GetTargetRotation(Target, transform);
            Turret.transform.rotation = Quaternion.Lerp(Turret.transform.rotation, TargetRotation, AgentBehaviour.RotationSpeed * Time.deltaTime);

            //FramesCount uses Time.deltatime so that it works with TimeScale
            FrameCount -= Time.deltaTime;
            if (FrameCount <= 0)
            {
                bFire = false;
            }

            //If turret is in specified range, change to shoot state.
            if (AgentFunctions.GetTargetAngle(Turret, Target, transform, AgentBehaviour.RotationAccuracy) && !bFire)
            {
                Transition(Shoot(), "Target In Range");
            }

            yield return(null);
        }
    }
Beispiel #2
0
    private void OnTriggerStay(Collider other)
    {
        //Very simple Tank avoidance.
        if (other.gameObject.tag == "Tank")
        {
            float Dir = AgentFunctions.AngleDir(transform.up, (transform.position - other.transform.position), transform.forward);

            if (Dir == 1)
            {
                Body.AddTorque(transform.up * TurnSpeed * -0.50f);
                Debug.DrawLine(transform.position, transform.position + transform.right * TurnSpeed * -2);
            }
            else
            {
                Body.AddTorque(transform.up * TurnSpeed * 0.50f);
                Debug.DrawLine(transform.position, transform.position + transform.right * TurnSpeed * 2);
            }
        }

        //Very simple Mine avoidance.
        if (other.gameObject.tag == "Mine")
        {
            float Dir = AgentFunctions.AngleDir(transform.up, (transform.position - other.transform.position), transform.forward);

            if (Dir == 1)
            {
                Body.AddTorque(transform.up * TurnSpeed * -1.0f);
                Debug.DrawLine(transform.position, transform.position + transform.right * TurnSpeed * -2);
            }
            else
            {
                Body.AddTorque(transform.up * TurnSpeed * 1.0f);
                Debug.DrawLine(transform.position, transform.position + transform.right * TurnSpeed * 2);
            }
        }
    }