コード例 #1
0
ファイル: NavMoveTo.cs プロジェクト: araya/ET-Plus
        private void moveToBlackboardKey()
        {
            object target = Blackboard.Get(blackboardKey);

            if (target == null)
            {
                stopAndCleanUp(false);
                return;
            }

            // get target location
            Vector3 destination = Vector3.zero;

            if (target is Transform)
            {
                if (updateFrequency >= 0.0f)
                {
                    destination = ((Transform)target).position;
                }
            }
            else if (target is Vector3)
            {
                destination = (Vector3)target;
            }
            else
            {
                Debug.LogWarning("NavMoveTo: Blackboard Key '" + this.blackboardKey + "' contained unsupported type '" + target.GetType());
                stopAndCleanUp(false);
                return;
            }

            // set new destination
            agent.destination = destination;

            bool destinationChanged = (agent.destination - lastDestination).sqrMagnitude > (DESTINATION_CHANGE_THRESHOLD * DESTINATION_CHANGE_THRESHOLD); //(destination - agent.destination).sqrMagnitude > (DESTINATION_CHANGE_THRESHOLD * DESTINATION_CHANGE_THRESHOLD);
            bool distanceChanged    = Mathf.Abs(agent.remainingDistance - lastDistance) > DESTINATION_CHANGE_THRESHOLD;

            // check if we are already at our goal and stop the task
            if (lastDistance < this.tolerance)
            {
                if (stopOnTolerance || (!destinationChanged && !distanceChanged))
                {
                    // reached the goal
                    stopAndCleanUp(true);
                    return;
                }
            }
            else if (!destinationChanged && !distanceChanged)
            {
                if (failedChecks++ > DESTINATION_CHANGE_MAX_CHECKS)
                {
                    // could not reach the goal for whatever reason
                    stopAndCleanUp(false);
                    return;
                }
            }
            else
            {
                failedChecks = 0;
            }

            lastDestination = agent.destination;
            lastDistance    = agent.remainingDistance;
        }