我是劉茗,一個專注各種有趣玩法的開發者。
本次帶來的是二段跳的教程。
在許多動作遊戲中,都有著【二段跳】這樣的遊戲系統。本文將教大家一個最快實現二段跳的方法。
在Character標頭檔案中,我們可以找到:
最大跳躍次數的定義
/**
* The max number of jumps the character can perform。
* Note that if JumpMaxHoldTime is non zero and StopJumping is not called, the player
* may be able to perform and unlimited number of jumps。 Therefore it is usually
* best to call StopJumping() when jump input has ceased (such as a button up event)。
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, Category=Character)
int32 JumpMaxCount;
在Character。cpp中,這個引數會在CanJumpInternal_Implementation函式中被使用。
因此,繼承Character類的物件可以修改上述引數來實現
當我們把這個引數改成2的時候……
二段跳已完成
!
透過修改這個引數,我們還可以實現三段跳、四段跳、各種跳都可以!
看到這裡,覺得不夠過癮的朋友們在這裡附上……
彩蛋
跳躍系統的分析與研究
首先看一下Jump函式:
這裡發現bPressedJump被設定成了true,那麼這個變數在哪裡被使用了呢?在CheckJumpInput函式中。
const bool bDidJump = CanJump() && CharacterMovement->DoJump(bClientUpdating);
在上面這一行程式碼中,我們發現這裡呼叫了CharacterMovement元件的DoJump函式。
跳轉到DoJump函式中
這裡對Velocity的Z值進行了賦值,以及設定了運動模式為Falling。
顯然——
這個便是Jump功能真正實現函數了。
此外,Character類還提供了一些藍圖介面供開發者定製。
/** Let blueprint know that we were launched */
UFUNCTION(BlueprintImplementableEvent)
void OnLaunched(FVector LaunchVelocity, bool bXYOverride, bool bZOverride);
/** Event fired when the character has just started jumping */
UFUNCTION(BlueprintNativeEvent, Category=Character)
void OnJumped();
在跳躍和落地時,可以新增一些定製化的功能。
最後祝熱愛自己事業的朋友們——
人(帥/美)( )大