Unreal Engine 4 —— GAS系统学习 (五) 为主角武器添加碰撞体

 

该人物使用的SkeletalMesh的模型是带有剑的碰撞体的,我们先在Physics里面进行删除,后再进行以下操作。

以下代码添加在角色头文件中:

 

1     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "TestProperties")
2     class UCapsuleComponent* capsuleBox;
3 
4     FScriptDelegate attackTouchDelegate;
5 
6     UFUNCTION()
7     void beginOverlap(UPrimitiveComponent* overlapComp,AActor* actor,UPrimitiveComponent* otherComp,int index,bool sweep,FHitResult &hit);

 

 

以下代码添加在CPP文件中:

 

 1 capsuleBox = CreateDefaultSubobject<UCapsuleComponent>("capsuleBox");
 2 capsuleBox->SetupAttachment(shibiSkeletal);
 9     capsuleBox->SetCapsuleHalfHeight(43.0f);
10     capsuleBox->SetCapsuleRadius(12.0f);
11 
12     capsuleBox->SetRelativeLocationAndRotation(FVector(0,-30,0), FQuat(FRotator(0,0,-90)));
13     attackTouchDelegate.BindUFunction(this,FName("beginOverlap"));
14     capsuleBox->OnComponentBeginOverlap.Add(attackTouchDelegate);
15 
18     capsuleBox->SetGenerateOverlapEvents(true);
19     capsuleBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
20     capsuleBox->SetCollisionObjectType(ECC_WorldDynamic);
21     capsuleBox->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Overlap);
22 
23     capsuleBox->AttachTo(shibiSkeletal, "weapon_r");

 

我们想让添加的碰撞体随着剑挥舞动所以得将碰撞体关联到人物骨骼的socket上,这里使用了capsuleBox->AttachTo(shibiSkeletal, "weapon_r");方法进行关联。

第一个参数是父类,名为“weapon_r”的socket是在shibiSkeletal组件上的,所以,第一个参数为shibiSekeltal,第二个参数是要关联的socket的名字。具体名字取骨骼模型中寻找。

 

然后设置大小,相对位置。

 

capsuleBox->SetCapsuleHalfHeight(43.0f);
capsuleBox->SetCapsuleRadius(12.0f);

capsuleBox->SetRelativeLocationAndRotation(FVector(0,-30,0), FQuat(FRotator(0,0,-90)));

 

再用FScriptDelegte进行动态代理,被代理的函数需要UFUNCTION进行反射,勿忘。

 

 1 void AShinbi::beginOverlap(UPrimitiveComponent* overlapComp, AActor* actor, UPrimitiveComponent* otherComp, int index, bool sweep, FHitResult& hit)
 2 {
 3     auto parent = actor->GetClass();
 4 
 5     if (actor != this) {
 6         if (otherComp) {
 7             GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Blue, otherComp->GetName());
 8         }
 9     }
10 }

 

在代理方法中,打印出剑碰撞到的组件名字。

开启碰撞,进行碰撞属性设置。

1     capsuleBox->SetGenerateOverlapEvents(true);
2     capsuleBox->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
3     capsuleBox->SetCollisionObjectType(ECC_WorldDynamic);
4     capsuleBox->SetCollisionResponseToChannel(ECC_Pawn, ECollisionResponse::ECR_Overlap);

只对PAWN类型有碰撞反馈,反馈类型是OVERLAP。

 

效果如下:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值