Unity标准化移动极慢

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

Unity Normalized Movement Extremely Slow

问题

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

  1. float targetMovingSpeed = IsRunning ? runSpeed : speed;
  2. // 从输入获取目标速度。
  3. Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);
  4. if (targetVelocity.magnitude > 1)
  5. {
  6. targetVelocity = targetVelocity.normalized;
  7. }
  8. // 应用移动。
  9. 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.

  1. float targetMovingSpeed = IsRunning ? runSpeed : speed;
  2. // Get targetVelocity from input.
  3. Vector2 targetVelocity = new Vector2( Input.GetAxis("Horizontal") * targetMovingSpeed, Input.GetAxis("Vertical") * targetMovingSpeed);
  4. if (targetVelocity.magnitude > 1)
  5. {
  6. targetVelocity = targetVelocity.normalized;
  7. }
  8. // Apply movement.
  9. rigidbody.velocity = transform.rotation * new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
  10. </details>
  11. # 答案1
  12. **得分**: 1
  13. 你将输入乘以 `targetMovingSpeed`,然后进行归一化,使其大小为 `1`
  14. 你更想这样做:
  15. // 参见 https://docs.unity3d.com/ScriptReference/Vector2.ClampMagnitude.html
  16. var input = Vector2.ClampMagnitude(new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")), 1f);
  17. var targetVelocity = input * targetMovingSpeed;
  18. rigidbody.velocity = transform.rotation * targetVelocity;
  19. <details>
  20. <summary>英文:</summary>
  21. You multiply the input by `targetMovingSpeed`, then you normalize it which will make its magnitude to be `1`.
  22. You rather want to do it like
  23. // see https://docs.unity3d.com/ScriptReference/Vector2.ClampMagnitude.html
  24. var input = Vector2.ClampMagnitude(new Vector2(Input.GetAxis(&quot;Horizontal&quot;), Input.GetAxis(&quot;Vertical&quot;)), 1f);
  25. var targetVelocity = input * targetMovingSpeed;
  26. rigidbody.velocity = transform.rotation * targetVelocity;
  27. </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:

确定