Unity Player下降非常缓慢

时间:2017-05-22 04:58:52

标签: c# unity3d

我为3D Platformer游戏创建了控件。不知怎的,玩家的速度非常慢。

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

所以我的代码就在这里:

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;
    }

有人知道该怎么做吗?

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

在这行代码中

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

取出

* Time.deltaTime

现在玩家正在正常下降。

答案 2 :(得分:1)

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

使用

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

这将起作用,或者只是使用AddForce驱动播放器。

答案 3 :(得分:0)

当你想使用重力计算时,在你的代码中改变刚体可以做到这一点。

 //rb.velocity = moveInput * moveSpeed;

例如,即使没有按下按钮,也会随着您的动作而扭曲。

使用类似的东西:

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

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

答案 4 :(得分:0)

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

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

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;
    }

}
相关问题