AI Perception Location and Rotation
- Greg Mladucky
- Mar 27, 2019
- 1 min read
By default Unreal's AI perception used the characters midpoint for its location and the controller for its rotation. This doesn't quite work with our AI as we want the deer's head to dictate what it is the animal is looking at. After a quick search through the forums we found the functions that control this. You can find that thread here:
https://answers.unrealengine.com/questions/743065/ai-perception-attachment.html
However, we ended up overriding the two functions that control the location and rotation directly as opposed to the GetActorViewPoint() function shown in the thread. These are the two functions below we inserted into our custom character header file:
virtual FRotator GetViewRotation() const override;
virtual FVector GetPawnViewLocation() const override;
Inside our .cpp file we used the code from the forum post but kept a call to the super if we get a character that for some odd reason has no mesh.
FVector AMEM_Character::GetPawnViewLocation() const
{
if (GetMesh() != nullptr)
{
return GetMesh()->GetSocketLocation("Socket_View");
}
return Super::GetPawnViewLocation();
}
FRotator AMEM_Character::GetViewRotation() const
{
if (GetMesh() != nullptr)
{
FRotator Rotation;
Rotation.Yaw = GetMesh()->GetSocketTransform("Socket_View", RTS_World).Rotator().Yaw;
return Rotation;
}
return Super::GetViewRotation();
}
Comments