AI Dodge Wiki
Dear Community,
I’ve put up a wiki on how to quickly and easily implement an AI dodge mechanic!
https://wiki.unrealengine./AI_Dodge,_Rotate_Vector_Along_Axis_By_Angle#AI_Dodge
I discuss:
-
Obtaining the perpedicular to the direction of the AI unit to its target
-
Picking which way to dodge
-
Choosing how far to dodge along the perpedicular
-
How to ensure the UE4 Navigation System will find the dodge point on the nav mesh
-
How to do all easily using FVector::RotateAngleAxis
**My C++ Code For You**
```
void AIDodge(bool DodgeRight=true, float Distance=256); //.h
void AYourAIClass::AIDodge(bool DodgeRight, float Distance)
{
//Location of unit who wants to dodge sideways, presumed to be facing target already
FVector UnitLocation = GetActorLocation();
FVector DirectionToActor = (OtherActor->GetActorLocation() - UnitLocation ).GetSafeNormal();
//Optional, Ensure UE4 Nav mesh point will be found.
DirectionToActor.Z = 0;
//Rotate the direction by 90 to get perpendicular of direction to actor
FVector Perpendicular = DirectionToActor.RotateAngleAxis(90,FVector(0,0,1));
//Dodging to relative Left or Right?
Perpendicular *= (DodgeRight) ? 1 : -1;
//Tell Unit to move 256 units along perpendicular
FVector GoalLocation = UnitLocation + Perpendicular * Distance;
//Tell unit to move to location
AAIController* AIControl = Cast<AAIController>(GetController());
if(AIControl)
{
AIControl->MoveToLocation(GoalLocation,0); //Optional Acceptance Radius
}
}
```
Enjoy!