예제 #1
0
        public override void OnInterrupt()
        {
            KoboldCharacterController controller = (graph as AIGraph).GetVariable <KoboldCharacterController>("controller");

            controller.inputDir  = Vector3.zero;
            controller.inputJump = false;
        }
예제 #2
0
        public override AIStatus OnProcess() {
            if (Time.timeSinceLevelLoad > recalculateTimer) {
                float giveUpTimerSave = giveUpTimer;
                OnInit();
                giveUpTimer = giveUpTimerSave;
            }
            if (alreadyInside) {
                return AIStatus.Failure;
            }
            GameObject targetGameObject = (graph as AIGraph).GetVariable<GameObject>("targetObject");
            Transform t = (graph as AIGraph).GetVariable<Transform>("transform");
            Vector3 pos = t.position;
            Vector3 desiredPos = Vector3.zero;
            if (pathNode < path.corners.Length) {
                desiredPos = path.corners[pathNode];
            } else {
                desiredPos = targetGameObject.transform.position;
            }
            Vector3 dir = (desiredPos - pos).GroundVector().normalized;
            Rigidbody r = (graph as AIGraph).GetVariable<Rigidbody>("rigidbody");
            Vector3 axis;
            float angle;
            Quaternion.FromToRotation(t.forward, dir).ToAngleAxis(out angle, out axis);
            r.AddTorque(Vector3.Project(r.angularVelocity, axis)*(graph as AIGraph).GetVariable<float>("UpdateDelay"));
            r.AddTorque(axis * angle * (graph as AIGraph).GetVariable<float>("UpdateDelay") * r.mass * 10f);

            KoboldCharacterController controller = (graph as AIGraph).GetVariable<KoboldCharacterController>("controller");
            controller.inputDir = dir;
            controller.inputJump = (desiredPos.y - pos.y) > 1f;

            // We made it inside!
            Collider c = targetGameObject.GetComponentInChildren<Collider>();
            if (c.ClosestPoint(pos) == pos) {
                return AIStatus.Success;
            }
            if (Vector3.Distance(pos, desiredPos) < 1f) {
                pathNode++;
                giveUpTimer = Time.timeSinceLevelLoad + giveUpTime;
                if (pathNode >= path.corners.Length) {
                    controller.inputDir = Vector3.zero;
                    controller.inputJump = false;
                    return AIStatus.Success;
                }
            }
            if (Time.timeSinceLevelLoad > giveUpTimer) {
                controller.inputDir = Vector3.zero;
                controller.inputJump = false;
                return AIStatus.Failure;
            }
            return AIStatus.Running;
        }
예제 #3
0
        public override AIStatus OnProcess()
        {
            //if (Time.timeSinceLevelLoad > recalculateTimer) {
            //float giveUpTimerSave = giveUpTimer;
            //OnInit();
            //giveUpTimer = giveUpTimerSave;
            //}
            if (foundPath == false)
            {
                return(AIStatus.Failure);
            }
            //if (pathNode >= path.corners.Length) {
            //return AIStatus.Failure;
            //}
            Transform t          = (graph as AIGraph).GetVariable <Transform>("transform");
            Vector3   pos        = t.position;
            Vector3   desiredPos = Vector3.zero;

            if (pathNode < path.corners.Length)
            {
                desiredPos = path.corners[pathNode];
            }
            else
            {
                GameObject targetGameObject = (graph as AIGraph).GetVariable <GameObject>("targetObject");
                if (targetGameObject == null)
                {
                    Vector3 targetPosition = (graph as AIGraph).GetVariable <Vector3>("targetObject");
                    if (targetPosition == null)
                    {
                        return(AIStatus.Failure);
                    }
                    desiredPos = targetPosition;
                }
                else
                {
                    desiredPos = targetGameObject.transform.position;
                }
            }
            Vector3   dir = (desiredPos - pos).GroundVector().normalized;
            Rigidbody r   = (graph as AIGraph).GetVariable <Rigidbody>("rigidbody");
            Vector3   axis;
            float     angle;

            Quaternion.FromToRotation(t.forward, dir).ToAngleAxis(out angle, out axis);
            //r.AddTorque(Vector3.Project(r.angularVelocity, axis)*(graph as AIGraph).GetVariable<float>("UpdateDelay"));
            r.AddTorque(axis * angle * (graph as AIGraph).GetVariable <float>("UpdateDelay") * r.mass * 10f);

            KoboldCharacterController controller = (graph as AIGraph).GetVariable <KoboldCharacterController>("controller");

            controller.inputDir  = dir;
            controller.inputJump = (desiredPos.y - pos.y) > 1f;
            Debug.DrawLine(t.position, desiredPos, Color.blue, (graph as AIGraph).GetVariable <float>("UpdateDelay"));

            if (Vector3.Distance(pos, desiredPos) < 1f)
            {
                pathNode++;
                giveUpTimer = Time.timeSinceLevelLoad + giveUpTime;
                //recalculateTimer = Time.timeSinceLevelLoad + recalculateTime;
                if (pathNode >= path.corners.Length)
                {
                    controller.inputDir  = Vector3.zero;
                    controller.inputJump = false;
                    return(AIStatus.Success);
                }
            }
            if (Time.timeSinceLevelLoad > giveUpTimer)
            {
                controller.inputDir  = Vector3.zero;
                controller.inputJump = false;
                return(AIStatus.Failure);
            }
            return(AIStatus.Running);
        }