C++攀岩.判断条件

在完成了组件绑定和创建自定义组件后,接下来就得判断是时候才可以使用我们的攀岩功能

我们可以先画出我们的思维导图,方便我们理清我们的判断

根据我们的思维导图来实现我们的判断功能

首先添加自定义模式

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;
}

这样我们的判断条件就写好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值