首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >改变Vector.Lerp Unity 5.6中的移动速度

改变Vector.Lerp Unity 5.6中的移动速度
EN

Stack Overflow用户
提问于 2018-01-02 07:59:53
回答 2查看 3.5K关注 0票数 1

我使用Vector3.Lerp在统一游戏中简单地将游戏对象从一个位置移动到另一个位置。下面是我的代码:

代码语言:javascript
运行
复制
public class playermovement : MonoBehaviour {


    public float timeTakenDuringLerp = 2f;

    private bool _isLerping;

    private Vector3 _startPosition;
    private Vector3 _endPosition;

    private float _timeStartedLerping;

    void StartLerping(int i)
    {
        _isLerping = true;
        _timeStartedLerping = Time.time  ; // adding 1 to time.time here makes it wait for 1 sec before starting

        //We set the start position to the current position, and the finish to 10 spaces in the 'forward' direction
        _startPosition = transform.position;
        _endPosition = new Vector3(transform.position.x + i,transform.position.y,transform.position.z);

    }

    void Update()
    {
        //When the user hits the spacebar, we start lerping
        if(Input.GetKey(KeyCode.Space))
        {
            int i = 65;
            StartLerping(i);
        }
    }

    //We do the actual interpolation in FixedUpdate(), since we're dealing with a rigidbody
    void FixedUpdate()
    {
        if(_isLerping)
        {
            //We want percentage = 0.0 when Time.time = _timeStartedLerping
            //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
            //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
            //"Time.time - _timeStartedLerping" is.
            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

            //Perform the actual lerping.  Notice that the first two parameters will always be the same
            //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
            //to start another lerp)
            transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete);

            //When we've completed the lerp, we set _isLerping to false
            if(percentageComplete >= 1.0f)
            {
                _isLerping = false;
            }
        }
    }


}

代码工作良好,游戏对象在两个点之间平稳地移动。但到达目的地需要1秒左右。我想让它走得更快。我尝试过降低浮点timeTakenDuringLerp的值,但是速度没有受到影响。我遵循了本教程,其中的解释也说为了改变速度而改变timeTakenDuringLerp变量,但是它在这里不起作用。

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-02 22:01:54

H℮y,谢谢你链接到我的博客!

降低timeTakenDuringLerp是正确的解决方案。这减少了物体从开始移动到完成所需的时间,这也是“它提高速度”的另一种说法。

如果有特定的速度希望对象移动,则需要将timeTakenDuringLerp设置为变量而不是常量,并将其设置为distance/speed。或者更好的是,不要使用Lerp,而是设置对象的velocity,让统一的物理引擎来处理它。

按照@Thalthanas的建议,将percentageComplete乘以一个常量是不正确的。这将导致lerping更新在lerping完成后继续进行。它还使代码难以理解,因为timeTakenDuringLerp不再是lerp期间所花费的时间。

我反复检查了我的代码,它确实有效,所以您所遇到的问题肯定在其他地方。或者你不小心增加了时间,这会降低速度?

票数 2
EN

Stack Overflow用户

发布于 2018-01-02 08:06:46

解决办法是

percentageComplete值与speed值相乘,

代码语言:javascript
运行
复制
transform.position = Vector3.Lerp (_startPosition, _endPosition, percentageComplete*speed); 

所以当你增加speed的时候,它会跑得更快。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48057491

复制
相关文章

相似问题

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