首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果超过-1/1,如何使Vector3标准化?

如果超过-1/1,如何使Vector3标准化?
EN

Stack Overflow用户
提问于 2019-12-19 12:36:39
回答 1查看 666关注 0票数 0

因此,我目前正在为一个角色创建运动,除了对角线运动加倍之外,一切都很好,因为它明显地将垂直和水平运动结合在一起。现在我确实尝试了规范Vector3,但是这导致了角色延迟停止。根据我读过的其他论坛,我认为这是因为它只有当它大于/小于-1/1时才应该正常化,但我不知道如何设置这个约束。请帮帮我!

代码语言:javascript
运行
复制
forwardInput = Input.GetAxis("Horizontal");
horizontalInput = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(horizontalInput, 0, forwardInput).normalized * speed * Time.deltaTime;
transform.Translate(movement);
EN

回答 1

Stack Overflow用户

发布于 2019-12-19 13:04:35

总是标准化的问题是,无论输入按多远,您都以相同的速度移动。

这在具有键盘输入的PC上没有问题,键盘输入是0-11,但如果使用控制器,则不会像预期的那样工作,因为如果原始输入向量的大小小于1,它也会将值集合起来,以防您正常化。

所以,是的,只有在输入向量的大小超过1的情况下,例如,当同时按两个方向时,才应该正常化,以保持一定的最大速度。为了不输入冗余代码,我宁愿将向量和赋值分离到不同的变量中,而是使用Vector3.Normalize

代码语言:javascript
运行
复制
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);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59409959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档