在完成了组件绑定和创建自定义组件后,接下来就得判断是时候才可以使用我们的攀岩功能
我们可以先画出我们的思维导图,方便我们理清我们的判断
根据我们的思维导图来实现我们的判断功能
首先添加自定义模式
UENUM(BlueprintType)
namespace ECustomMovementMode
{
enum Type
{
MOVE_Climb UMETA(DisplayName = "Climb Mode")
};
}
判断是否处于攀爬模式。
void AClimbingSystemCharacter::ClimbNorRockStared(const FInputActionValue& Value)
{
if(!CustomMovementComponent) return;
if(!CustomMovementComponent->IsOrNotClimbingModel())//判断是否处于攀爬模式
{
CustomMovementComponent->ToggleClimbingModel(true);
}
else
{
CustomMovementComponent->ToggleClimbingModel(false);
}
}
bool UCustomMovementComponent::IsOrNotClimbingModel() const
{
return MovementMode == MOVE_Custom && CustomMovementMode == ECustomMovementMode::MOVE_Climb;
}
void UCustomMovementComponent::StartClimbing()
{
SetMovementMode(MOVE_Custom, ECustomMovementMode::MOVE_Climb);
}
void UCustomMovementComponent::StopClimbing()
{
SetMovementMode(MOVE_Falling);
}
void UCustomMovementComponent::ToggleClimbingModel(bool ClimbModel)
{
if (ClimbModel)
{
if (CanStartClimbing())
{
StartClimbing();
}
}
if(!ClimbModel)
{
StopClimbing();
}
}
大致框架写完后,我们就可以写判断进行攀爬的条件。判断进行攀爬的条件有两个条件
1.从character前面生成一个碰撞胶囊,当碰撞到墙壁时返回碰撞点信息
2.从character的眼睛出生成一条射线,当碰撞到墙壁时返回碰撞点信息
条件2的目的是为了防止出现墙壁比我们的character矮。
bool UCustomMovementComponent::TraceClimbableSurface()
{
const FVector StartOffset = UpdatedComponent->GetForwardVector() * 30.f;
const FVector Start = UpdatedComponent->GetComponentLocation() + StartOffset;//要检查角色前面的位置,所以要在此基础上加上多余的偏移量
const FVector End = Start + UpdatedComponent->GetForwardVector();
ClimbableSurfacesResults = GetTraceClimbableSurface(Start, End,true,false);
return !ClimbableSurfacesResults.IsEmpty();//如果是空的话返回ture,不是空的返回false
//Debug::Print(TEXT("fuck1"));
}
FHitResult UCustomMovementComponent::LineTraceClimbaleSurface(float TraceDistance, float TraceStartOffset, bool bShowDebug , bool bDrawPersistentShapes)
{
const FVector ComponentLocation = UpdatedComponent->GetComponentLocation();
const FVector EyeHeightOffset = UpdatedComponent->GetUpVector() * (CharacterOwner->BaseEyeHeight + TraceStartOffset);
const FVector Start = ComponentLocation + EyeHeightOffset;
const FVector End = Start + UpdatedComponent->GetForwardVector() * TraceDistance;
return GetLineTraceClimbaleSurface(Start, End,bShowDebug,bDrawPersistentShapes);
}
bool UCustomMovementComponent::CanStartClimbing()
{
if (IsFalling()) return false;//如果下降是真的
if (!TraceClimbableSurface()) return false;
if (!LineTraceClimbaleSurface(100,0,true,false).bBlockingHit) return false;
return true;
}
这样我们的判断条件就写好了