首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity3D中的动量和惯性

Unity3D中的动量和惯性
EN

Stack Overflow用户
提问于 2022-06-21 20:44:39
回答 1查看 153关注 0票数 0

所以,当我松开钥匙的时候,控制器停了下来,就像撞到墙上一样,我试着改变它,但唯一改变的是,每当我按下一个键,它就会被扔到外层空间:

代码语言:javascript
复制
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 newMovement = transform.right * x + transform.forward * z;
    momentum = new Vector3(characterController.velocity.x, 0, characterController.velocity.z);
    newMovement.y = 0;
 if (!newMovement.normalized.Equals(momentum.normalized))
        {
            
            Debug.Log("new" + newMovement.normalized);
            Debug.Log(momentum.normalized);

            momentum = (momentum.magnitude - 2f) > 0 ? momentum.normalized * (momentum.magnitude - 2f) : Vector3.zero;
            

            if (newMovement.x == momentum.x)
                momentum.x = 0;



            if (newMovement.z == momentum.z)
                momentum.z = 0;



            
        }
        else
            momentum = Vector3.zero;
        characterController.Move((newMovement * speed + velocity + momentum) * Time.deltaTime);

另外,由于某种原因,即使有时两个向量相等,它们也会通过if语句(我尝试使用!=)(这两个向量都记录在if语句的前2行)。

EN

回答 1

Stack Overflow用户

发布于 2022-06-22 09:28:47

使用https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html,它将逐渐减缓移动到零,这取决于smoothTime的值

代码语言:javascript
复制
public float smoothTime = 0.3F;
private Vector3 velocity = Vector3.zero;
private Vector3 newMovement;

void Update()
{
    newMovement = transform.right * x + transform.forward * z;
    transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72706921

复制
相关文章

相似问题

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