因此,我目前正在为一个角色创建运动,除了对角线运动加倍之外,一切都很好,因为它明显地将垂直和水平运动结合在一起。现在我确实尝试了规范Vector3,但是这导致了角色延迟停止。根据我读过的其他论坛,我认为这是因为它只有当它大于/小于-1/1时才应该正常化,但我不知道如何设置这个约束。请帮帮我!
forwardInput = Input.GetAxis("Horizontal");
horizontalInput = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontalInput, 0, forwardInput).normalized * speed * Time.deltaTime;
transform.Translate(movement);发布于 2019-12-19 13:04:35
总是标准化的问题是,无论输入按多远,您都以相同的速度移动。
这在具有键盘输入的PC上没有问题,键盘输入是0、-1或1,但如果使用控制器,则不会像预期的那样工作,因为如果原始输入向量的大小小于1,它也会将值集合起来,以防您正常化。
所以,是的,只有在输入向量的大小超过1的情况下,例如,当同时按两个方向时,才应该正常化,以保持一定的最大速度。为了不输入冗余代码,我宁愿将向量和赋值分离到不同的变量中,而是使用Vector3.Normalize:
forwardInput = Input.GetAxis("Horizontal");
horizontalInput = Input.GetAxis("Vertical");
var inputVector = new Vector3(horizontalInput, 0, forwardInput);
// note that a magnitude is always positive, there is no -1
// if magnitude > 1 then implicitely also sqrMagnitude > 1 and the other way round
// sqrMagnitude is faster to access then magnitude and for this check
// here provides the same result
if(inputVector.sqrMagnitude > 1) inputVector.Normalize();
var movement = inputVector * speed * Time.deltaTime;
transform.Translate(movement);https://stackoverflow.com/questions/59409959
复制相似问题