Unity标准化移动极慢

huangapple go评论73阅读模式
英文:

Unity Normalized Movement Extremely Slow

问题

如标题所述,当我对我的移动进行归一化处理(以防止对角线移动增加幅度)时,即使大幅增加移动速度,我的移动速度仍然非常缓慢。我觉得这与归一化何时应用有关,但我不确定。

float targetMovingSpeed = IsRunning ? runSpeed : speed;

// 从输入获取目标速度。
Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);

if (targetVelocity.magnitude > 1)
{
    targetVelocity = targetVelocity.normalized;
}

// 应用移动。
rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
英文:

As said in the title, when I normalize my movement (to prevent diagonal from having increased magnitude) my movement speed is extremely slow even when increasing it drastically. I feel like it has to do with when the normalize is being applied but I'm not sure.

        float targetMovingSpeed = IsRunning ? runSpeed : speed;

        // Get targetVelocity from input.
        Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);

        if (targetVelocity.magnitude > 1)
        {
            targetVelocity = targetVelocity.normalized;
        }

        // Apply movement.
        rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);

</details>


# 答案1
**得分**: 1

你将输入乘以 `targetMovingSpeed`,然后进行归一化,使其大小为 `1`。

你更想这样做:

    // 参见 https://docs.unity3d.com/ScriptReference/Vector2.ClampMagnitude.html
    var input = Vector2.ClampMagnitude(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), 1f);
    var targetVelocity = input * targetMovingSpeed;

    rigidbody.velocity = transform.rotation * targetVelocity;

<details>
<summary>英文:</summary>

You multiply the input by `targetMovingSpeed`, then you normalize it which will make its magnitude to be `1`.

You rather want to do it like

    // see https://docs.unity3d.com/ScriptReference/Vector2.ClampMagnitude.html
    var input = Vector2.ClampMagnitude(new Vector2(Input.GetAxis(&quot;Horizontal&quot;), Input.GetAxis(&quot;Vertical&quot;)), 1f);
    var targetVelocity = input * targetMovingSpeed;

    rigidbody.velocity = transform.rotation * targetVelocity;



</details>



huangapple
  • 本文由 发表于 2023年7月5日 00:57:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定