【问题标题】:Unity Player falling very slowlyUnity Player 下落非常缓慢
【发布时间】:2021-06-22 01:24:47
【问题描述】:

我为 3D 平台游戏创建了控件。不知何故,玩家下落的速度非常缓慢。

我的玩家对象有 2 个组件,默认胶囊碰撞器和默认刚体。我没有改变任何东西。

所以我的代码在这里:

float movementSpeed = 8;
float currentMovementSpeed;
float speedSmoothTime = 0.1f;
float turnSmoothTime = 0.2f;
float jumpPower = 5;
float airControlPercentage = 0.2f;
float turnSmoothVelocity;
float speedSmoothVelocity;
bool isGrounded;

    private void FixedUpdate()
    {
        isGrounded = GroundCheck(); // Is player grounded?

        Vector2 inputDirection = (new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"))).normalized;

        if (Input.GetButtonDown("Jump") && isGrounded) // Jump handling
            Debug.Log("Player Jump");

        if (inputDirection != Vector2.zero)
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, Mathf.Atan2(inputDirection.x, inputDirection.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime)); // Rotate

        currentMovementSpeed = Mathf.SmoothDamp(currentMovementSpeed, movementSpeed * inputDirection.magnitude, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));

        playerRigid.velocity = transform.forward * currentMovementSpeed + Vector3.up * playerRigid.velocity.y * Time.deltaTime; // Move

        currentMovementSpeed = (new Vector2(playerRigid.velocity.x, playerRigid.velocity.z)).magnitude;
    }

    private float GetModifiedSmoothTime(float smoothTime) // Limit the control while in air
    {
        if (isGrounded)
            return smoothTime;

        if (airControlPercentage == 0)
            return float.MaxValue;

        return smoothTime / airControlPercentage;
    }

    private bool GroundCheck() // Player is grounded?
    {
        if (true)
            return true;

        return false;
    }

有人知道在这里做什么吗?

【问题讨论】:

  • 您是否尝试更改Rigidbody 组件中的质量?
  • 同时查看 Rigidbodydrag 值:如果太高,可能会导致移动缓慢。

标签: c# unity3d


【解决方案1】:

这可能与您当前的重力有关。签入edit -> project settings -> physics 你的重力值。在我的情况下是-9,81。将其更改为更高的值,看看会发生什么。

【讨论】:

    【解决方案2】:

    我终于明白了。如何解决:

    在这行代码中

    playerRigid.velocity = transform.forward * currentMovementSpeed + Vector3.up * playerRigid.velocity.y * Time.deltaTime;
    

    拿出来

    * Time.deltaTime
    

    现在玩家正在正确下落。

    【讨论】:

      【解决方案3】:

      实际上,transform.forward 会影响身体的y 组件,从而影响作用在身体上的重力。

      使用

      rb.velocity = new Vector3(horizontal , -1 , vertical) * speed ; .

      它会起作用,或者干脆使用AddForce来驱动播放器。

      【讨论】:

        【解决方案4】:

        当您想使用重力计算时,可以在代码中更改刚体。

         //rb.velocity = moveInput * moveSpeed;
        

        例如,即使没有按下按钮,也会影响您的动作。

        使用类似的东西:

           if(Input.GetButtonDown("Jump") && Mathf.Abs(rb.velocity.y) < 0.001f)
            {
                rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
            }
        

        将简单地在计算之上添加力,而不是在之前更改它们

        【讨论】:

          【解决方案5】:

          在我的例子中,我的游戏角色缓慢下降的问题是通过在 FixedUpdate() 函数中消除对物理系统的不必要调用来解决的。例如,当操纵杆的 X 轴和/或 Y 轴处于零位时。

          只有当绝对操纵杆值(- 和 +)超过最小值(在我的例子中为 0.04)时,我才会调用 addForce、velocity、rb 变换等。如果没有这些测试,每次调用 FixedUpdate() 时都会执行这些物理调用。这似乎使物理系统过载,因为与此同时,物理也在处理重力等。 请注意,仅在 Unity 输入系统中设置操纵杆的死区并不能解决此问题。

          void FixedUpdate()
          {
              if (Mathf.Abs(stick.x) > 0.04) // prevent unnecessary physics call.
              {
                  rb.transform.eulerAngles = rb.transform.eulerAngles - new Vector3(0, stick.x * Time.deltaTime * RotationSpeed * -1, 0);
              }
              if (Mathf.Abs(stick.y) > 0.04) // prevent unnecessary physics call.
              {
                  rb.velocity = transform.forward * stick.y * Speed * Time.deltaTime;
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-06-07
            • 2010-10-24
            • 1970-01-01
            • 2021-11-11
            • 2019-08-11
            • 2010-10-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多