MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / OnUpdate

Method OnUpdate

Source/Engine/AI/BehaviorTreeNodes.cpp:536–585  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

534#endif
535
536void BehaviorTreeMoveToNode::State::OnUpdate()
537{
538 if (Result != BehaviorUpdateResult::Running)
539 return;
540 PROFILE_CPU();
541
542 // Get agent properties
543 const Vector3 agentLocation = Agent->GetPosition();
544 float movementSpeed;
545 if (!Node->MovementSpeed.TryGet(Knowledge, movementSpeed))
546 movementSpeed = 100;
547 float agentRadius = 30.0f, agentHeight = 100.0f;
548 Node->GetAgentSize(Agent, agentRadius, agentHeight);
549
550 // Test if agent reached the next path segment
551 Vector3 pathSegmentEnd = Path[TargetPathIndex];
552 const Vector3 agentLocationOnPath = agentLocation + AgentOffset;
553 const bool isLastSegment = TargetPathIndex + 1 == Path.Count();
554 float testRadius;
555 if (isLastSegment)
556 testRadius = agentRadius + Node->AcceptableRadius;
557 else
558 testRadius = agentRadius * 0.05f + Math::Max(agentRadius - NavAgentRadius, 0.0f); // 5% threshold of agent radius and diff between navmesh vs agent radius as threshold for path segments reaching
559 const float acceptableHeightPercentage = 1.05f;
560 const float testHeight = agentHeight * acceptableHeightPercentage;
561 const Vector3 toGoal = agentLocationOnPath - pathSegmentEnd;
562 const Real toGoalHeightDiff = (toGoal * UpVector).SumValues();
563 if (toGoal.Length() <= testRadius && toGoalHeightDiff <= testHeight)
564 {
565 TargetPathIndex++;
566 if (TargetPathIndex == Path.Count())
567 {
568 // Goal reached!
569 Result = BehaviorUpdateResult::Success;
570 return;
571 }
572 pathSegmentEnd = Path[TargetPathIndex];
573 }
574
575 // Move agent
576 const float maxMove = movementSpeed * Time::Update.DeltaTime.GetTotalSeconds();
577 if (maxMove <= ZeroTolerance)
578 return;
579 const Vector3 move = Vector3::MoveTowards(agentLocationOnPath, pathSegmentEnd, maxMove) - agentLocationOnPath;
580 if (Node->Move(Agent, move))
581 {
582 // Move failed!
583 Result = BehaviorUpdateResult::Failed;
584 }
585}
586
587void BehaviorTreeInvertDecorator::PostUpdate(const BehaviorUpdateContext& context, BehaviorUpdateResult& result)
588{

Callers

nothing calls this directly

Calls 9

MoveTowardsFunction · 0.85
GetAgentSizeMethod · 0.80
MaxFunction · 0.50
GetPositionMethod · 0.45
TryGetMethod · 0.45
CountMethod · 0.45
SumValuesMethod · 0.45
LengthMethod · 0.45
MoveMethod · 0.45

Tested by

no test coverage detected