UE4 ACharacter部分方法介绍
原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明出處。
創(chuàng)建一個(gè)基于Character的藍(lán)圖,可以看到左側(cè)可以覆蓋一些方法,針對(duì)這些方法在代碼層面做一個(gè)介紹。
如下圖,寫這篇文章的目的其實(shí)希望大家知道引擎原生提供的一些方法,能夠更快的寫邏輯。
防止各位同仁自造輪子。
本文只介紹Character的事件,Actor的事件在上篇文章中講過了。UE4 Actor事件接口詳解
Character事件列表
- CanJump
- OnJumped
- OnLanded
- OnLaunched
- On Walking Off Ledge
- OnStartCrouch/OnEndCrouch蹲下等函數(shù)
- OnMovementModeChanged
- Possessed
- Unpossessed
- UpdateCustomMovement
CanJump
是否可跳躍,跳躍之前的條件判斷
檢查角色是否可以在當(dāng)前狀態(tài)下跳躍。跳躍之前的一些你的邏輯判斷。
描述:是否可以跳躍?
源碼:其實(shí)由上面的源碼可以看出,由BlueprintCallable所修飾的CanJump虛方法其實(shí)調(diào)用的就是由
BlueprintNativeEvent所修飾的CanJumpInternal(), CanJumpInternal()這個(gè)方法藍(lán)圖可以重載實(shí)現(xiàn),
當(dāng)藍(lán)圖中重載了之后C++ 的CanJumpInternal()將不再會(huì)調(diào)用。
C++默認(rèn)的CanJumpInternal_Implementation()(默認(rèn)這個(gè)方法的實(shí)現(xiàn)體會(huì)有些條件判斷,
比如不是蹲著的時(shí)候返回true,蹲著的時(shí)候是不允許跳躍的;判斷跳躍是否超過了最大跳躍限制次數(shù),比如二段跳);
如果是你的C++想重載的話,請(qǐng)重載CanJump()邏輯,而不是CanJumpInternal()。
當(dāng)觸發(fā)一次跳躍,執(zhí)行邏輯依次為
1>CanJump? 我可以跳嗎?
2>CanJump? 我可以跳嗎?
3>OnJump 跳起來了
4>OnLanded 我落地了
如果藍(lán)圖中直接實(shí)現(xiàn)了CanJump, return true的話,Character上面的JumpMaxCount(最大限制跳躍次數(shù))將失去效果,需要自己增加這部分的邏輯。如果你發(fā)現(xiàn)你的二段跳功能失效了,懷疑是不是這里的問題,或者搜索JumpMaxCount也能找到原因。
OnJumped
事件在角色剛開始跳躍時(shí)觸發(fā),當(dāng)你觸發(fā)跳躍的一瞬間,就會(huì)調(diào)用這個(gè)函數(shù)
在C++中調(diào)用
在藍(lán)圖中重寫OnJump
OnLanded
事件在角色剛開始跳躍落地時(shí)觸發(fā),當(dāng)你接觸Floor的一瞬間,就會(huì)調(diào)用這個(gè)函數(shù)。 參數(shù)是當(dāng)時(shí)落地的坐標(biāo)的一些參數(shù),比如有具體的位置,落地時(shí)候地表的物理材質(zhì)等信息。
在C++中你想重載寫一些邏輯的話,重載virtual void Landed(const FHitResult& Hit);
在藍(lán)圖中你想重載寫一些邏輯的話,直接實(shí)現(xiàn)OnLanded(const FHitResult& Hit)即可。
OnLaunched
發(fā)射角色,和Jump有點(diǎn)像,這個(gè)是會(huì)給一個(gè)力。應(yīng)用到就像CF里面生化模式跳到某個(gè)傳送門一下跳老高的那種效果。我覺得就是為FPS游戲設(shè)計(jì)的該功能
我在場(chǎng)景中放了一個(gè)傳送門,很丑,用了個(gè)火的粒子。
當(dāng)角色與這個(gè)TriigerBox發(fā)生Overlap時(shí)候,會(huì)將角色彈射起來,給角色一個(gè)向上的力。就像CF里面那種效果。下面是在關(guān)卡藍(lán)圖里面的實(shí)現(xiàn)。
回到藍(lán)圖中的OnLaunched方法介紹上
UFUNCTION(BlueprintImplementableEvent)
void OnLaunched(FVector LaunchVelocity, bool bXYOverride, bool bZOverride)
其實(shí)這個(gè)方法觸發(fā)的時(shí)候,就是角色被調(diào)用了LaunchCharacter,之后會(huì)調(diào)用CharacterMovement->Launch(FinalVel),
然后調(diào)用讓藍(lán)圖知道一下這個(gè)角色被發(fā)射了。
On Walking Off Ledge
觸發(fā)現(xiàn)象是當(dāng)你的walk切換到fall,比如從高處掉下的時(shí)候會(huì)調(diào)用。
我認(rèn)為可以做一些從步行->跌倒的功能。
官方解釋不包括跳躍或其他可能導(dǎo)致運(yùn)動(dòng)模式中發(fā)生相同過渡的事物。
下面是Character.h中的相關(guān)代碼
/*** Event fired when the Character is walking off a surface and is about to fall because CharacterMovement->CurrentFloor became unwalkable.* If CharacterMovement->MovementMode does not change during this event then the character will automatically start falling afterwards.* @note Z velocity is zero during walking movement, and will be here as well. Another velocity can be computed here if desired and will be used when starting to fall.** @param PreviousFloorImpactNormal Normal of the previous walkable floor.* @param PreviousFloorContactNormal Normal of the contact with the previous walkable floor.* @param PreviousLocation Previous character location before movement off the ledge.* @param TimeTick Time delta of movement update resulting in moving off the ledge.*/UFUNCTION(BlueprintNativeEvent, Category=Character)void OnWalkingOffLedge(const FVector& PreviousFloorImpactNormal, const FVector& PreviousFloorContactNormal, const FVector& PreviousLocation, float TimeDelta);virtual void OnWalkingOffLedge_Implementation(const FVector& PreviousFloorImpactNormal, const FVector& PreviousFloorContactNormal, const FVector& PreviousLocation, float TimeDelta);OnStartCrouch/OnEndCrouch蹲下等函數(shù)
想要角色開始蹲下, 需要滿足下面條件
1>需要將UCharacterMovementComponent里面的NavAgentProps參數(shù)中bCanCrouch設(shè)置為true
2>需要調(diào)用Crouch進(jìn)入蹲下狀態(tài),相反的是調(diào)用UnCrouch退出蹲下狀態(tài)
角色蹲下后走到邊坡不能進(jìn)入自由落體,怎么解決?
1>將UCharacterMovementComponent對(duì)象的bCanWalkOffLedgesWhenCrouching設(shè)置為true
下面是CharacterMovementComponent.h中關(guān)于蹲下的相關(guān)參數(shù)聲明(請(qǐng)看具體的注釋理解)
//蹲下時(shí)候的最大移動(dòng)速度,也就是說蹲下后走的慢了。沒錯(cuò)符合物理真實(shí),這很UE4。UPROPERTY(Category="Character Movement: Walking", EditAnywhere, BlueprintReadWrite, meta=(ClampMin="0", UIMin="0"))float MaxWalkSpeedCrouched;//蹲下時(shí)候角色的Capsule的高度UPROPERTY(Category="Character Movement (General Settings)", EditAnywhere, BlueprintReadOnly, meta=(ClampMin="0", UIMin="0"))float CrouchedHalfHeight;//這個(gè)比較有意思,默認(rèn)為false。如果false的話走到邊坡你會(huì)發(fā)現(xiàn)沒法掉下去。當(dāng)為true的時(shí)候就能掉下去了。//并且也會(huì)觸發(fā)On Walking Off LedgeUPROPERTY(Category="Character Movement: Walking", EditAnywhere, BlueprintReadWrite)uint8 bCanWalkOffLedgesWhenCrouching:1;下面是Character.h中關(guān)于蹲下的相關(guān)函數(shù)聲明
//調(diào)用該方法讓 蹲下UFUNCTION(BlueprintCallable, Category=Character, meta=(HidePin="bClientSimulation"))virtual void Crouch(bool bClientSimulation = false);//調(diào)用該方法讓角色 停止蹲下UFUNCTION(BlueprintCallable, Category=Character, meta=(HidePin="bClientSimulation"))virtual void UnCrouch(bool bClientSimulation = false);//可以蹲下嗎?UFUNCTION(BlueprintCallable, Category=Character)virtual bool CanCrouch() const;//當(dāng)角起身的時(shí)候調(diào)用 調(diào)用了UnCrouch之后virtual void OnEndCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//藍(lán)圖版本的OnEndCrouchUFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnEndCrouch", ScriptName="OnEndCrouch"))void K2_OnEndCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//角色蹲下的時(shí)候調(diào)用, 調(diào)用了Crouch之后virtual void OnStartCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//藍(lán)圖版本的OnStartCrouchUFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnStartCrouch", ScriptName="OnStartCrouch"))void K2_OnStartCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);OnMovementModeChanged
這個(gè)比較好理解,Character上CharacterMovement中的變量
UPROPERTY(Category=“Character Movement: MovementMode”, BlueprintReadOnly)
TEnumAsByte MovementMode;
也就是說當(dāng)這個(gè)枚舉發(fā)生變化的時(shí)候會(huì)有個(gè)通知
下面是C++的代碼部分
//MovementMode改變//參數(shù):PrevMovementMode 更改之前是啥模式//參數(shù):PreviousCustomMode 更改之前的自定義模式是啥。注意是自定義模式。比如絕地求生的climbing攀爬模式virtual void OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode = 0);//MovementMode改變的時(shí)候廣播這個(gè)多播代理UPROPERTY(BlueprintAssignable, Category=Character)FMovementModeChangedSignature MovementModeChangedDelegate;//藍(lán)圖中用的,就是在上面的OnMovementModeChanged中調(diào)用的。//參數(shù):PrevMovementMode 更改之前是啥模式//參數(shù):NewMovementMode 新的模式//參數(shù):PrevCustomMode 更改之前的自定義模式是啥。注意是自定義模式//參數(shù):NewCustomMode 更改之后的自定義模式是啥模式。比如絕地求生的climbing攀爬模式UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnMovementModeChanged", ScriptName="OnMovementModeChanged"))void K2_OnMovementModeChanged(EMovementMode PrevMovementMode, EMovementMode NewMovementMode, uint8 PrevCustomMode, uint8 NewCustomMode);Possessed
Controller和Pawn其實(shí)是一個(gè)相互持有的關(guān)系,比如有想切換操控的角色的功能,可以用這個(gè)實(shí)現(xiàn)。
那么應(yīng)該怎么去實(shí)現(xiàn)切換角色的功能呢?
1>調(diào)用Controller->Possess(傳一個(gè)Pawn的對(duì)象即可完成切換角色的功能)
2>不建議調(diào)用角色身上的PossessedBy, 可能會(huì)遇到一些問題,因?yàn)镃ontroller的部分邏輯沒有被執(zhí)行到。
3>往往切換角色功能會(huì)伴隨著要spawn一個(gè)新的character邏輯,然后再將這個(gè)新spawn出來的character進(jìn)行Controller->Possess(新spawn的character),再將老的character進(jìn)行Destroy();
Character繼承自Pawn,下面Pawn.h的代碼
//當(dāng)Controller調(diào)用Possess的時(shí)候呢,會(huì)調(diào)用到這個(gè)方法virtual void PossessedBy(AController* NewController);//在ReceivePossessed中調(diào)用的,目的是為了通知藍(lán)圖UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName= "Possessed"))void ReceivePossessed(AController* NewController);/** Event called when the Pawn is no longer possessed by a Controller. */UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName= "Unpossessed"))void ReceiveUnpossessed(AController* OldController);//當(dāng)Controller調(diào)用UnPossess的時(shí)候調(diào)用Pawn的UnPossessedvirtual void UnPossessed();下面是Controller.h的代碼, 可以看到下面?zhèn)z方法都已經(jīng)是final,不允許在進(jìn)行重寫了。
/*** Handles attaching this controller to the specified pawn.* Only runs on the network authority (where HasAuthority() returns true).* @param InPawn The Pawn to be possessed.* @see HasAuthority()*/UFUNCTION(BlueprintCallable, BlueprintAuthorityOnly, Category=Pawn, meta=(Keywords="set controller"))virtual void Possess(APawn* InPawn) final; // DEPRECATED(4.22, "Posssess is marked virtual final as you should now be overriding OnPossess instead")/** Called to unpossess our pawn for any reason that is not the pawn being destroyed (destruction handled by PawnDestroyed()). */UFUNCTION(BlueprintCallable, Category=Pawn, meta=(Keywords="set controller"))virtual void UnPossess() final; // DEPRECATED(4.22, "Posssess is marked virtual final as you should now be overriding OnUnPossess instead")Unpossessed
和上面的Possessed相反
比如此時(shí)我有個(gè)需求,不想控制這個(gè)角色了,怎么辦?
1 >關(guān)閉角色的Input, DisableInput(Controller和Character身上都有,調(diào)用一次)
2>來個(gè)狠的,直接調(diào)用Unpossessed.
UpdateCustomMovement
這個(gè)函數(shù)是當(dāng)MovementMode設(shè)置成Custom的時(shí)候,這個(gè)方法會(huì)調(diào)用。下面是堆棧信息。這個(gè)是通過UCharacterMovementComponent::PhysCustom()過來的
相對(duì)應(yīng)的UCharacterMovementComponent中還有PhysWalking/PhysNavWalking/PhysFlying/PhysSwimming/還有PhysCustom等幾個(gè)方法。這個(gè)里面可以重載了實(shí)現(xiàn)自定義模式下的邏輯。
下面是Character.h中對(duì)這個(gè)方法的定義
/*** Event for implementing custom character movement mode. Called by CharacterMovement if MovementMode is set to Custom.* @note C++ code should override UCharacterMovementComponent::PhysCustom() instead.* @see UCharacterMovementComponent::PhysCustom()*/UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="UpdateCustomMovement", ScriptName="UpdateCustomMovement"))void K2_UpdateCustomMovement(float DeltaTime);下面是一個(gè)關(guān)于K2_UpdateCustomMovement調(diào)用的一個(gè)堆棧信息
謝謝!創(chuàng)作不易,大俠請(qǐng)留步… 動(dòng)起可愛的雙手,來個(gè)贊再走唄 (???←?)
總結(jié)
以上是生活随笔為你收集整理的UE4 ACharacter部分方法介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql character_set_
- 下一篇: 美丽桌面墙纸自动换