【问题标题】:I added coyote time to my jump in my 2d platformer(where you can jump shortly after jumping off the ledge), but it's not working我在我的 2d 平台游戏中为我的跳跃添加了土狼时间(您可以在跳下窗台后不久跳跃),但它不起作用
【发布时间】:2022-11-03 19:02:12
【问题描述】:

所有设置都在那里,代码对我来说似乎是正确的,但它只是不起作用。 这是涉及郊狼时间的代码:

[SerializeField, Range(0f, 0.3f)] private float _coyoteTime = 0.2f;

private float coyoteCounter

private void FixedUpdate()
{
    onGround = ground.GetOnGround();
    velocity = body.velocity;

    if(onGround && body.velocity.y == 0)
    {
        jumpPhase = 0;
        coyoteCounter = _coyoteTime;
        isJumping = false;
    }
    else
    {
        coyoteCounter -= Time.deltaTime;
    }

    private void JumpAction()
{
    if(coyoteCounter > 0f || jumpPhase < maxAirJumps && isJumping)
    {
        if(isJumping)
        {
            jumpPhase += 1;
        }

        jumpBufferCounter = 0;
        coyoteCounter = 0;
        jumpSpeed = Mathf.Sqrt(-2f * Physics2D.gravity.y * jumpHeight);
        isJumping = true;
        if(velocity.y > 0f)
        {
            jumpSpeed = Mathf.Max(jumpSpeed - velocity.y, 0f);
        }
        velocity.y += jumpSpeed;
    }
}

如果有人可以向我解释出了什么问题,那将不胜感激。谢谢!

【问题讨论】:

  • 出色地。您的跳跃动作永远不会被调用,并且在您的固定更新中。什么是土狼时间?它到底在做什么和不做什么
  • 除了@BugFinder 所说,您的代码没有正确的右括号,没有正确使用vector2(它是一个值类型,不是引用)并且没有将速度设置回body 任何地方。这是您的代码的真实表示吗?

标签: c# unity3d 2d


【解决方案1】:

首先,您不需要为此使用固定更新,您的 ground.GetOnGround 可能已填充 FixedUpdate 本身。当您处理 AddForce 一段时间而不仅仅是一帧时,FixedUpdate 最有用。 其次,如果您使用的是 FixedUpdate,请使用 fixedDeltaTime 而不是 deltaTime。

这是效果的准系统,您可以自己将多跳添加到逻辑中。

// Will set from checking the ground contact on update
bool IsOnGround = true;
bool WasOnGround = true;
bool LedgeFall = false;
bool IsJumping = false;

bool ThereIsGroundContact()
{
   // Check if character is contact with ground with raycast or overlaps
}

void OnUpdate()
{
   IsOnGround = ThereIsGroundContact();

   if (IsOnGround && !WasOnGround)
   {
      OnLand();
   }

   // if falling from ledge
   if (!IsOnGround && WasOnGround && velocity.y <= 0) 
   {
      LedgeFall = true;
      CoyoteTimer = 0;
   }
        
   if (LedgeFall){
       CoyoteTimer += Time.deltaTime;
   }
    
   if (LedgeFall && CoyoteTimer >= _CoyoteTime){
       LedgeFall = false;
   }

   // You can process jump input here
   if ((IsOnGround || LedgeFall) && JumpInputPressed)
       IsJumping = true;

   WasOnGround = IsOnGround;
}

void OnLand()
{
   LedgeFall = false;
   IsJumping = false;
   CoyoteTimer = 0;
}

希望这可以帮助

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多