示例#1
0
        public override void StartBehavior(Agent agent)
        {
            Context context = agentsContexts[agent];

            // Recalculate path in case things have changed
            InteractionSpotType interactionSpotType = context.target.entityType.interactionSpotType;

            context.targetPosition        = interactionSpotType.GetInteractionSpot(agent, context.target, agent.decider.CurrentMapping, true, false);
            context.rotateTowardsLocation = interactionSpotType.GetRotateTowardsLocation(agent, context.target, agent.decider.CurrentMapping);
            context.distanceTolerance     = interactionSpotType.GetDistanceTolerance(agent, context.target, agent.decider.CurrentMapping,
                                                                                     defaultDistanceTolerance);

            // Set destination
            // TODO: Send in path since we need to calculate it when picking target
            // TODO: Should always use targetPosition - doesn't make sense to go into target
            // TODO: targetPosition will be PositiveInfinity if it couldn't find a spot - interrupt mapping
            agent.movementType.SetSpeed(agent, context.currentSpeed);
            agent.movementType.SetStopped(agent, false);
            agent.movementType.SetDestination(agent, Target(context.target, context.targetPosition));

            if (agent.animationType.ImplementsAnimationRigging())
            {
                agent.animationType.SetRigWeight(agent, 0, 1);
                agent.animationType.WarpRigConstraintTargetTo(agent, 0, agent.transform.InverseTransformPoint(context.targetPosition),
                                                              Quaternion.identity);
            }
        }
示例#2
0
        public override void UpdateBehavior(Agent agent)
        {
            Context context = agentsContexts[agent];

            // Burn off energy based on how fast Agent was moving
            // TODO: First time should use afterStartWaitTime
            if (reduceEnergy)
            {
                context.energyAT.ChangeLevelUsingChangeCurve(agent, context.currentSpeed, context.movementSpeedAT, afterUpdateWaitTime, false);
            }

            // TODO: Can't always ignore y - Could be moving under or above the target position
            //float distanceToTargetPosition = Vector3.Distance(agent.transform.position, context.targetPosition);
            float distanceToTargetPosition = DistanceIgnoreY(agent.transform.position, context.targetPosition);

            // Make sure agent can continue going to destination
            if (context.target == null || !context.target.gameObject.activeInHierarchy)
            {
                return;
            }

            if (changeSpeedDuringBehavior)
            {
                Selector valueSelector = context.attributeSelectors[0];
                context.currentSpeed = valueSelector.GetFloatValue(agent, agent.decider.CurrentMapping);
                agent.movementType.SetSpeed(agent, context.currentSpeed);
                Debug.Log(context.currentSpeed);
            }

            if (agent.animationType.ImplementsAnimationRigging())
            {
                agent.animationType.WarpRigConstraintTargetTo(agent, 0, agent.transform.InverseTransformPoint(context.targetPosition),
                                                              Quaternion.identity);
            }

            // If target is an agent need to keep updating targetPosition
            //Agent targetAgent = context.target.GetComponent<Agent>();
            //if (context.target != null && targetAgent != null && distanceToTargetPosition > distanceTolerance)
            if (recalculateDestination && distanceToTargetPosition > context.distanceTolerance)
            {
                // Recalculate path in case things have changed
                InteractionSpotType interactionSpotType = context.target.entityType.interactionSpotType;
                context.targetPosition = interactionSpotType.GetInteractionSpot(agent, context.target, agent.decider.CurrentMapping, true, false);
                agent.movementType.SetDestination(agent, context.targetPosition);

                // Set in behavior so target gizmo will show correctly
                agent.behavior.SetTargetPosition(context.targetPosition);
            }

            // Rotate towards the target at the end
            // TODO: This is jerky - need to figure out how to do this every frame and gradually
            //       Maybe set something on agent and have it run in agent.Update
            if (rotateTowardsTarget && context.target != null && distanceToTargetPosition < context.distanceTolerance)
            {
                agent.transform.rotation = GetRotation(agent.transform, context.rotateTowardsLocation);
            }
        }