日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UE4 ACharacter部分方法介绍

發布時間:2023/12/16 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UE4 ACharacter部分方法介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原創內容,轉載請注明出處。

創建一個基于Character的藍圖,可以看到左側可以覆蓋一些方法,針對這些方法在代碼層面做一個介紹。
如下圖,寫這篇文章的目的其實希望大家知道引擎原生提供的一些方法,能夠更快的寫邏輯。
防止各位同仁自造輪子。

本文只介紹Character的事件,Actor的事件在上篇文章中講過了。UE4 Actor事件接口詳解

Character事件列表

  • CanJump
  • OnJumped
  • OnLanded
  • OnLaunched
  • On Walking Off Ledge
  • OnStartCrouch/OnEndCrouch蹲下等函數
  • OnMovementModeChanged
  • Possessed
  • Unpossessed
  • UpdateCustomMovement

CanJump

是否可跳躍,跳躍之前的條件判斷



檢查角色是否可以在當前狀態下跳躍。跳躍之前的一些你的邏輯判斷。

描述:是否可以跳躍?
源碼:其實由上面的源碼可以看出,由BlueprintCallable所修飾的CanJump虛方法其實調用的就是由
BlueprintNativeEvent所修飾的CanJumpInternal(), CanJumpInternal()這個方法藍圖可以重載實現,
當藍圖中重載了之后C++ 的CanJumpInternal()將不再會調用。
C++默認的CanJumpInternal_Implementation()(默認這個方法的實現體會有些條件判斷,
比如不是蹲著的時候返回true,蹲著的時候是不允許跳躍的;判斷跳躍是否超過了最大跳躍限制次數,比如二段跳);
如果是你的C++想重載的話,請重載CanJump()邏輯,而不是CanJumpInternal()。

返回值:bool:是否可以在當前狀態跳轉

當觸發一次跳躍,執行邏輯依次為
1>CanJump? 我可以跳嗎?
2>CanJump? 我可以跳嗎?
3>OnJump 跳起來了
4>OnLanded 我落地了
如果藍圖中直接實現了CanJump, return true的話,Character上面的JumpMaxCount(最大限制跳躍次數)將失去效果,需要自己增加這部分的邏輯。如果你發現你的二段跳功能失效了,懷疑是不是這里的問題,或者搜索JumpMaxCount也能找到原因。

OnJumped

事件在角色剛開始跳躍時觸發,當你觸發跳躍的一瞬間,就會調用這個函數

在C++中調用

在藍圖中重寫OnJump

OnLanded

事件在角色剛開始跳躍落地時觸發,當你接觸Floor的一瞬間,就會調用這個函數。 參數是當時落地的坐標的一些參數,比如有具體的位置,落地時候地表的物理材質等信息。


在C++中你想重載寫一些邏輯的話,重載virtual void Landed(const FHitResult& Hit);
在藍圖中你想重載寫一些邏輯的話,直接實現OnLanded(const FHitResult& Hit)即可。

OnLaunched

發射角色,和Jump有點像,這個是會給一個力。應用到就像CF里面生化模式跳到某個傳送門一下跳老高的那種效果。我覺得就是為FPS游戲設計的該功能


我在場景中放了一個傳送門,很丑,用了個火的粒子。
當角色與這個TriigerBox發生Overlap時候,會將角色彈射起來,給角色一個向上的力。就像CF里面那種效果。下面是在關卡藍圖里面的實現。

回到藍圖中的OnLaunched方法介紹上
UFUNCTION(BlueprintImplementableEvent)
void OnLaunched(FVector LaunchVelocity, bool bXYOverride, bool bZOverride)

其實這個方法觸發的時候,就是角色被調用了LaunchCharacter,之后會調用CharacterMovement->Launch(FinalVel),
然后調用讓藍圖知道一下這個角色被發射了。

On Walking Off Ledge

觸發現象是當你的walk切換到fall,比如從高處掉下的時候會調用。
我認為可以做一些從步行->跌倒的功能。
官方解釋不包括跳躍或其他可能導致運動模式中發生相同過渡的事物。

下面是Character.h中的相關代碼

/*** 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蹲下等函數

想要角色開始蹲下, 需要滿足下面條件
1>需要將UCharacterMovementComponent里面的NavAgentProps參數中bCanCrouch設置為true
2>需要調用Crouch進入蹲下狀態,相反的是調用UnCrouch退出蹲下狀態

角色蹲下后走到邊坡不能進入自由落體,怎么解決?
1>將UCharacterMovementComponent對象的bCanWalkOffLedgesWhenCrouching設置為true

下面是CharacterMovementComponent.h中關于蹲下的相關參數聲明(請看具體的注釋理解)

//蹲下時候的最大移動速度,也就是說蹲下后走的慢了。沒錯符合物理真實,這很UE4。UPROPERTY(Category="Character Movement: Walking", EditAnywhere, BlueprintReadWrite, meta=(ClampMin="0", UIMin="0"))float MaxWalkSpeedCrouched;//蹲下時候角色的Capsule的高度UPROPERTY(Category="Character Movement (General Settings)", EditAnywhere, BlueprintReadOnly, meta=(ClampMin="0", UIMin="0"))float CrouchedHalfHeight;//這個比較有意思,默認為false。如果false的話走到邊坡你會發現沒法掉下去。當為true的時候就能掉下去了。//并且也會觸發On Walking Off LedgeUPROPERTY(Category="Character Movement: Walking", EditAnywhere, BlueprintReadWrite)uint8 bCanWalkOffLedgesWhenCrouching:1;

下面是Character.h中關于蹲下的相關函數聲明

//調用該方法讓 蹲下UFUNCTION(BlueprintCallable, Category=Character, meta=(HidePin="bClientSimulation"))virtual void Crouch(bool bClientSimulation = false);//調用該方法讓角色 停止蹲下UFUNCTION(BlueprintCallable, Category=Character, meta=(HidePin="bClientSimulation"))virtual void UnCrouch(bool bClientSimulation = false);//可以蹲下嗎?UFUNCTION(BlueprintCallable, Category=Character)virtual bool CanCrouch() const;//當角起身的時候調用 調用了UnCrouch之后virtual void OnEndCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//藍圖版本的OnEndCrouchUFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnEndCrouch", ScriptName="OnEndCrouch"))void K2_OnEndCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//角色蹲下的時候調用, 調用了Crouch之后virtual void OnStartCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);//藍圖版本的OnStartCrouchUFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnStartCrouch", ScriptName="OnStartCrouch"))void K2_OnStartCrouch(float HalfHeightAdjust, float ScaledHalfHeightAdjust);

OnMovementModeChanged

這個比較好理解,Character上CharacterMovement中的變量
UPROPERTY(Category=“Character Movement: MovementMode”, BlueprintReadOnly)
TEnumAsByte MovementMode;
也就是說當這個枚舉發生變化的時候會有個通知

下面是C++的代碼部分

//MovementMode改變//參數:PrevMovementMode 更改之前是啥模式//參數:PreviousCustomMode 更改之前的自定義模式是啥。注意是自定義模式。比如絕地求生的climbing攀爬模式virtual void OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode = 0);//MovementMode改變的時候廣播這個多播代理UPROPERTY(BlueprintAssignable, Category=Character)FMovementModeChangedSignature MovementModeChangedDelegate;//藍圖中用的,就是在上面的OnMovementModeChanged中調用的。//參數:PrevMovementMode 更改之前是啥模式//參數:NewMovementMode 新的模式//參數:PrevCustomMode 更改之前的自定義模式是啥。注意是自定義模式//參數:NewCustomMode 更改之后的自定義模式是啥模式。比如絕地求生的climbing攀爬模式UFUNCTION(BlueprintImplementableEvent, meta=(DisplayName="OnMovementModeChanged", ScriptName="OnMovementModeChanged"))void K2_OnMovementModeChanged(EMovementMode PrevMovementMode, EMovementMode NewMovementMode, uint8 PrevCustomMode, uint8 NewCustomMode);

Possessed

Controller和Pawn其實是一個相互持有的關系,比如有想切換操控的角色的功能,可以用這個實現。

那么應該怎么去實現切換角色的功能呢?
1>調用Controller->Possess(傳一個Pawn的對象即可完成切換角色的功能)
2>不建議調用角色身上的PossessedBy, 可能會遇到一些問題,因為Controller的部分邏輯沒有被執行到。
3>往往切換角色功能會伴隨著要spawn一個新的character邏輯,然后再將這個新spawn出來的character進行Controller->Possess(新spawn的character),再將老的character進行Destroy();

Character繼承自Pawn,下面Pawn.h的代碼

//當Controller調用Possess的時候呢,會調用到這個方法virtual void PossessedBy(AController* NewController);//在ReceivePossessed中調用的,目的是為了通知藍圖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);//當Controller調用UnPossess的時候調用Pawn的UnPossessedvirtual void UnPossessed();

下面是Controller.h的代碼, 可以看到下面倆方法都已經是final,不允許在進行重寫了。

/*** 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相反
比如此時我有個需求,不想控制這個角色了,怎么辦?
1 >關閉角色的Input, DisableInput(Controller和Character身上都有,調用一次)
2>來個狠的,直接調用Unpossessed.

UpdateCustomMovement

這個函數是當MovementMode設置成Custom的時候,這個方法會調用。下面是堆棧信息。這個是通過UCharacterMovementComponent::PhysCustom()過來的
相對應的UCharacterMovementComponent中還有PhysWalking/PhysNavWalking/PhysFlying/PhysSwimming/還有PhysCustom等幾個方法。這個里面可以重載了實現自定義模式下的邏輯。

下面是Character.h中對這個方法的定義

/*** 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);

下面是一個關于K2_UpdateCustomMovement調用的一個堆棧信息

謝謝!創作不易,大俠請留步… 動起可愛的雙手,來個贊再走唄 (???←?)

總結

以上是生活随笔為你收集整理的UE4 ACharacter部分方法介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。