首页 > 代码库 > 【转】unity 移动物体到指定位置的四种方法
【转】unity 移动物体到指定位置的四种方法
http://blog.csdn.net/lcy0221/article/details/44040739
方法1:使用Vector3.MoveTowards
[csharp] view plain copy
- </pre><pre name="code" class="csharp">void Update ()
- {
- float step = speed * Time.deltaTime;
- gameObject.transform.localPosition = Vector3.MoveTowards(gameObject.transform.localPosition, new Vector3(10, -3, 50), step);
- }
方法2:使用插值
[csharp] view plain copy
- void Update ()
- {
- float step = speed * Time.deltaTime;
- gameObject.transform.localPosition =new Vector3(Mathf.Lerp(gameObject.transform.localPosition.x, 10, step),Mathf.Lerp(gameObject.transform.localPosition.y, -3, step),Mathf.Lerp(gameObject.transform.localPosition.z, 50, step));//插值算法也可以
- }
方法3:使用iTween
[csharp] view plain copy
- iTween.MoveTo(m_UIbgCamera, iTween.Hash("x", -20,
- "y", -3,
- "z", 50,
- "time", 1.0,
- "islocal", true
- ));
方法4:使用协程
[csharp] view plain copy
- StartCoroutine(MoveToPosition());
[csharp] view plain copy
- IEnumerator MoveToPosition()
- {
- GameObject m_UIbgCamera = GameObject.Find("UI/FengMian/UIbgCamera");
- while (m_UIbgCamera.transform.localPosition != new Vector3(-5, -3, 50))
- {
- m_UIbgCamera.transform.localPosition = Vector3.MoveTowards(m_UIbgCamera.transform.localPosition, new Vector3(-20, -3, 50), 10 * Time.deltaTime);
- yield return 0;
- }
- }
【转】unity 移动物体到指定位置的四种方法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。