您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 7浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity3d在一定时间内将物体移动到指定位置

程序员正茂 发布时间:2020-08-06 14:16:03 ,浏览量:7

方法一 

    private IEnumerator MoveTo(Transform tr, Vector3 pos, float time)
    {
        float t = 0;
        while(true)
        {
            t += Time.deltaTime;
            float a = t/ time;
            tr.position = Vector3.Lerp(tr.position, pos, a);
            if (a >= 1.0f)
                break;
            yield return null;
        }
        
    }

方法二

    private IEnumerator MoveTo(Transform tr, Vector3 pos, float time)
    {
        float t = 0;
        Vector3 startPos = tr.position;
        while(true)
        {
            t += Time.deltaTime;
            float a = t/ time;
            tr.position = Vector3.Lerp(startPos, pos, a);
            if (a >= 1.0f)
                break;
            yield return null;
        }
        
    }

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.1807s