zoukankan      html  css  js  c++  java
  • unity3d Vector3.Lerp解析

    Vector3.Lerp:http://www.ceeger.com/Script/Vector3/Vector3.Lerp.html

    手册中描述的不是很详细,什么叫“按照数字t在from到to之间插值”???t代表什么鬼?还得自己测试一下才知道

    我以前这样用过:

    from.position = Vector3.Lerp(from.position, to.position, Time.deltaTime);

    或者想要快一些我就这样:

    from.position = Vector3.Lerp(from.position, to.position, Time.deltaTime * 2f);

    不知道大家有没有像我这样用过!第三个参数取值范围0~1,其实就是距离百分比,比如填0.5f,那么就是取A点到B点的中间位置

    如果像我上面那种方法去使用,就会导致物体在移动的时候会越来越慢

    假设A点到B点距离是10,第三个参数t我填0.5f,那么插值一次后距离就减少了一半,在执行一次,又减少了一半距离

    当然,如果需求就是这样的话直接用就行了,但是如果需求是匀速平滑到某点,这咋办呢?

    既然都知道第三个参数其实就是距离百分比了,那我们自己算一下距离百分比不就行了吗?

     1     public Transform from;
     2     public Transform to;
     3     public float mMoveTime;
     4     private Vector3 mStartPos;
     5     private float t;
     6 
     7     private bool mIsPlay = false;
     8 
     9     void Update()
    10     {
    11         if (!mIsPlay)
    12             return;
    13 
    14         t += 1f / mMoveTime * Time.deltaTime;
    15         from.position = Vector3.Lerp(mStartPos, to.position, t);
    16     }
    17     
    18     void OnGUI()
    19     {
    20         if(GUI.Button(new Rect(100,100,100,30),"play"))
    21         {
    22             mStartPos = from.position;
    23             mIsPlay = true;
    24         }
    25     }

    看了上面的介绍,相信对Vector3.Lerp有些了解了!

    如果我想要他移动到指定位置后继续保持匀速运动怎么做呢?也许你会说用Vector3.Lerp完全可以啊!

    可是你别忘了,它的取值范围是0~1,也就是说>1的值它会忽略掉,我测试了一下的确如此

    那看来只能自己实现了!这其实很简单,下面我们就来自己实现一遍

    1     void Update()
    2     {
    3         if (!mIsPlay)
    4             return;
    5 
    6         t += 1f / mMoveTime * Time.deltaTime;
    7         //from.position = Vector3.Lerp(mStartPos, to.position, t);
    8         from.position = mStartPos + (to.position - mStartPos) * t;
    9     }

     原文链接:http://www.cnblogs.com/shenggege/p/5658650.html

  • 相关阅读:
    Java 读取某个目录下所有文件、文件夹
    poi根据excel模板导出Excel
    UML关系(泛化,实现,依赖,关联(聚合,组合))
    使用Git下载Hadoop的到本地Eclipse开发环境
    maven command to create your application
    Roo中的@Version
    oracle 给用户赋表空间
    hiberante 二级缓存设置
    procedure
    Oracle综合数据库管理命令集
  • 原文地址:https://www.cnblogs.com/shenggege/p/5658650.html
Copyright © 2011-2022 走看看