英文:
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("Horizontal"), Input.GetAxis("Vertical")), 1f);
var targetVelocity = input * targetMovingSpeed;
rigidbody.velocity = transform.rotation * targetVelocity;
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论