看了一下终于发现了跳跃的关键代码

  1. bool UCharacterMovementComponent::DoJump(bool bReplayingMoves)
  2. {
  3. if ( CharacterOwner && CharacterOwner->CanJump() )
  4. {
  5. // Don't jump if we can't move up/down.
  6. if (!bConstrainToPlane || FMath::Abs(PlaneConstraintNormal.Z) != .f)
  7. {
  8. Velocity.Z = JumpZVelocity;
  9. SetMovementMode(MOVE_Falling);
  10. return true;
  11. }
  12. }
  13.  
  14. return false;
  15. }

这里跳跃就和JumpZVelocity联系在一起了,同时运动状态改成了Falling(我认为这里设置Falling是不对的,因为在空中有上升还有下落两个状态),不过MovementComponent有判断停止下落的函数,可以在状态机里直接用。

当然判断是否可以跳跃就是另一回事了,你也可以自己写一个跳跃函数。

首先是CharactorMovementComponent中的DoJump(),里面检测一下能不能跳跃,(即是否允许角色Z方向的移动,因为要兼容别的类型的游戏。
),之后在ACharacter中DoJump()中与CanJump()进行与运算,CanJump返回CanJumpInternal(),CanJumpInternal()是个事件,所以我们需要

去看CanJumpInternal_Implementation(),果然CanJumpInternal_Implementation是个虚函数,所以修改跳跃逻辑就修改这个函数就好了。

看一下CanJumpInternal_Implementation的逻辑

  1. bool ACharacter::CanJumpInternal_Implementation() const
  2. {
  3. const bool bCanHoldToJumpHigher = (GetJumpMaxHoldTime() > 0.0f) && IsJumpProvidingForce();
  4.  
  5. return !bIsCrouched && CharacterMovement && (CharacterMovement->IsMovingOnGround() || bCanHoldToJumpHigher) && CharacterMovement->IsJumpAllowed() && !CharacterMovement->bWantsToCrouch;
  6. }

!bIsCrouched角色的运行模式不能为蹲

CharacterMovement->IsMovingOnGround() 确定角色是否还处于走路状态

bCanHoldToJumpHigher的是判断当按下跳跃键的时候,是否还能到达最高点

CharacterMovement->IsJumpAllowed() 角色运动组件是否可以跳

!CharacterMovement->bWantsToCrouch不能正在做下蹲动作

另外补充一下相关代码

  1. float UCharacterMovementComponent::GetMaxJumpHeight() const
  2. {
  3. const float Gravity = GetGravityZ();
  4. if (FMath::Abs(Gravity) > KINDA_SMALL_NUMBER)
  5. {
  6. return FMath::Square(JumpZVelocity) / (-.f * Gravity);
  7. }
  8. else
  9. {
  10. return .f;
  11. }
  12. }

一个重力下落公式

  1. void UCharacterMovementComponent::JumpOff(AActor* MovementBaseActor)
  2. {
  3. if ( !bPerformingJumpOff )
  4. {
  5. bPerformingJumpOff = true;
  6. if ( CharacterOwner )
  7. {
  8. const float MaxSpeed = GetMaxSpeed() * 0.85f;
  9. Velocity += MaxSpeed * GetBestDirectionOffActor(MovementBaseActor);
  10. if ( Velocity.Size2D() > MaxSpeed )
  11. {
  12. Velocity = MaxSpeed * Velocity.GetSafeNormal();
  13. }
  14. Velocity.Z = JumpOffJumpZFactor * JumpZVelocity;
  15. SetMovementMode(MOVE_Falling);
  16. }
  17. bPerformingJumpOff = false;
  18. }
  19. }

----------------------------------------------------------------------------------------------

如何实现:

首先在你的角色类的头文件中加入

virtual bool CanJumpInternal_Implementation() const override;

覆盖CanJumpInternal事件。

之后在Cpp文件中加入

  1. bool AThirdPersonCharacter::CanJumpInternal_Implementation() const
  2. {
  3. const bool bCanHoldToJumpHigher = (GetJumpMaxHoldTime() > 0.0f) && IsJumpProvidingForce();
  4.  
  5. //return !bIsCrouched && CharacterMovement && (CharacterMovement->IsMovingOnGround() || bCanHoldToJumpHigher) && CharacterMovement->IsJumpAllowed() && !CharacterMovement->bWantsToCrouch;
  6.   
  7. return !bIsCrouched && CharacterMovement && CharacterMovement->IsJumpAllowed() && !CharacterMovement->bWantsToCrouch;
  8. }

我把(CharacterMovement->IsMovingOnGround() || bCanHoldToJumpHigher)删掉了,这里就替换上你的二段跳逻辑即可

在头文件中加入2个用于判断二段跳的变量

  1. //最大跳跃次数
  2. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Jump")
  3. int32 MaxJumpNum = ;
  4.  
  5. //当前跳跃次数
  6. int32 JumpNum=2;

当然把JumNum在构造函数中赋值才是正确选择

覆盖事件

  1. //virtual void OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode = 0) override;
  2.  
  3. virtual void Landed(const FHitResult& Hit) override;

2个都可以用,第一个功能更加强大

然后在Cpp文件里:

  1. void AThirdPersonCharacter::Jump()
  2. {
  3. if (JumpNum>)
  4. {
  5. bPressedJump = true;
  6. JumpKeyHoldTime = 0.0f;
  7. JumpNum = JumpNum - ;
  8. }
  9. }
  10.  
  11. void AThirdPersonCharacter::Landed(const FHitResult& Hit)
  12. {
  13. Super::Landed(Hit);
  14. JumpNum = MaxJumpNum;
  15. }

当然之前的按键事件绑定也需要修改一下

  1. void AThirdPersonCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
  2. {
  3. // Set up gameplay key bindings
  4. check(InputComponent);
      //把这个Jump改为你自己刚才定义的Jump
  5. InputComponent->BindAction("Jump", IE_Pressed, this, &AThirdPersonCharacter::Jump);
  6. InputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
  7.  
  8. InputComponent->BindAxis("MoveForward", this, &AThirdPersonCharacter::MoveForward);
  9. InputComponent->BindAxis("MoveRight", this, &AThirdPersonCharacter::MoveRight);
  10.  
  11. // We have 2 versions of the rotation bindings to handle different kinds of devices differently
  12. // "turn" handles devices that provide an absolute delta, such as a mouse.
  13. // "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
  14. InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
  15. InputComponent->BindAxis("TurnRate", this, &AThirdPersonCharacter::TurnAtRate);
  16. InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
  17. InputComponent->BindAxis("LookUpRate", this, &AThirdPersonCharacter::LookUpAtRate);
  18.  
  19. // handle touch devices
  20. InputComponent->BindTouch(IE_Pressed, this, &AThirdPersonCharacter::TouchStarted);
  21. InputComponent->BindTouch(IE_Released, this, &AThirdPersonCharacter::TouchStopped);
  22. }

看Ue4角色代码——跳跃与实现二段跳的更多相关文章

  1. unity3D角色代码控制问题

    ///////////////2015/07/06//////// ///////////////by xbw////////////// //////////////环境 unity4.6.1// ...

  2. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码

    首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...

  3. 看图写代码---看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >>

    看图写代码 阅读<<Audio/Video Connectivity Solutions for Virtex-II Pro and Virtex-4 FPGAs >> 1.S ...

  4. 【前端模板之路】一、重构的兄弟说:我才不想看你的代码!把HTML给我交出来!

    写在前面 随着前端领域的发展和社会化分工的需要,继前端攻城湿之后,又一重要岗位横空出世——重构攻城湿!所谓的重构攻城湿,他们的一大特点之一,就是精通CSS配置文件的编写...前端攻城湿跟重构攻城湿是一 ...

  5. (转)【前端模板之路】一、重构的兄弟说:我才不想看你的代码!把HTML给我交出来!

    原文地址:http://www.cnblogs.com/chyingp/archive/2013/06/30/front-end-tmplate-start.html 写在前面 随着前端领域的发展和社 ...

  6. 我的Android进阶之旅------>真正在公司看几天代码的感触

    仅以此文来回顾这一周我的工作情况,以及由此而触发的感想. ============================================================= 来到新公司5天了, ...

  7. php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort)

    php实现把数组排成最小的数(核心是排序)(看别人的代码其实也没那么难)(把php代码也看一下)(implode("",$numbers);)(usort) 一.总结 核心是排序 ...

  8. 由阿里巴巴一道笔试题看Java静态代码块、静态函数、动态代码块、构造函数等的执行顺序

    一.阿里巴巴笔试题: public class Test { public static int k = 0; public static Test t1 = new Test("t1&qu ...

  9. 怎么看 EOS 的代码最爽?

    进入 EOS 的世界之前,愉快地看系统代码是第一步,试了 Visual Studio / Source Insight / Understand / Sublime 等多款 IDE / 编辑器后,强烈 ...

随机推荐

  1. 用Python套接字创建HTTP客户与服务器程序

    最近在学习python,网络编程中,python寥寥几句,就可以创建一个服务端和客户端程序: 服务端: import sockets = socket.socket()host = socket.ge ...

  2. ios电话/密码/验证码/身份证的正则表达式

    // 一 .电话号码正则表达式 -(BOOL)testPhoneNumber:(NSString *)text { NSString *regex =@"(13[0-9]|0[1-9]|0[ ...

  3. mongodb3.2配置文件yaml格式 详解

    mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...

  4. 使用xib需要记得的小问题

    1. 图片 加载 图片上的label 不显示, 最后是因为xib 里位置动了 图片跑到最上层盖住了labe 2. 加载xib 有时候会崩 或加载不出来  先查看xib 是否有多余控件 3. 查看关联 ...

  5. hibernate之处理视图

    近期,我去用hibernate去创建视图, 发现无法进立建立视图, 为啥? 个人去尝试去,却发现无法很好的完成, 因为hibernate的作用类似视图 后解决方案是: 1.用传统的方式去处理 2.写存 ...

  6. php文件上传进度条例子

    <?php session_start(); ?> <!DOCTYPE html> <html lang="zh-CN"> <head&g ...

  7. poj 1004:Financial Management(水题,求平均数)

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: ...

  8. javascript中求浏览器窗口可视区域兼容性写法

    1.浏览器窗口可视区域大小 1.1 对于IE9+.Chrome.Firefox.Opera 以及 Safari:•  window.innerHeight - 浏览器窗口的内部高度•  window. ...

  9. [LeetCode] Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  10. phpcms v9中调用栏目及调用多个子栏目中的文章列表

    调用一个指定栏目列表:            {pc:content action="lists" catid="6" order="id DESC& ...