角色設(shè)置
為了使角色能夠“抓取”武器,需要在骨骼中添加功能---socket,即插槽。
找到放置武器的位置,右鍵添加插槽即可(項目不同可能示例截圖不一致,但做法相同)

添加武器插槽.png
放置后,直接綁定會發(fā)現(xiàn)武器位置和想象中不太一樣;這里我們右鍵為socket添加預(yù)覽,通過預(yù)覽調(diào)整武器。

武器位置調(diào)整.png
藍(lán)圖實現(xiàn)
武器拿到手中主要通過AttachToComponent方法,既可以在武器藍(lán)圖腳本實現(xiàn)也可以在角色腳本中實現(xiàn),原理相同。
- 首先為武器添加事件OnComponentBeginOverlap;
- 然后檢測碰撞物體是否為角色
- AttachToComponent
-
(有時需要取消物體與角色的碰撞或其它調(diào)整,視具體情況而定)
如下:
這里注意AttachToComponent的具體設(shè)置,target為attach的物體,parent為被attach的scence component,此處為武器的藍(lán)圖腳本,因此target為self;parent應(yīng)設(shè)置在角色的mesh上。規(guī)則為Snap to Target
藍(lán)圖
代碼實現(xiàn)
void AChilikoCollectActor::NotifyActorBeginOverlap(AActor* OtherActor)
{
Super::NotifyActorBeginOverlap(OtherActor);
//碰撞后提示、特效等處理
if (Role == ROLE_Authority)
{
AChilikoCharacter* MyCharacter = Cast<AChilikoCharacter>(OtherActor);
if (MyCharacter)
{
// 拾取前的處理,如播放動畫/按鍵提示等。
AttachToComponent(MyCharacter->GetRootComponent(), FAttachmentTransformRules::SnapToTargetNotIncludingScale, FName(TEXT("hand_rSocket")));
// 拾取后的處理
}
}
}
當(dāng)然,也可以通過其它碰撞處理方法實現(xiàn)。
