zoukankan      html  css  js  c++  java
  • unity 移动物体到指定位置的四种方法 【精确移动到指定位置,再也不是计算距离了,物体可以高速移动】

    方法1:使用Vector3.MoveTowards

    [csharp] view plain copy
     
    1. </pre><pre name="code" class="csharp">void Update ()   
    2. {  
    3.     float step = speed * Time.deltaTime;  
    4.     gameObject.transform.localPosition = Vector3.MoveTowards(gameObject.transform.localPosition, new Vector3(10, -3, 50), step);  
    5. }  
    方法2:使用插值
    [csharp] view plain copy
     
    1. void Update ()   
    2. {  
    3.     float step = speed * Time.deltaTime;  
    4.     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));//插值算法也可以  
    5. }  

    方法3:使用iTween

    [csharp] view plain copy
     
    1. iTween.MoveTo(m_UIbgCamera, iTween.Hash("x",     -20,  
    2.                                                 "y",     -3,  
    3.                                                 "z",     50,  
    4.                                                 "time",  1.0,  
    5.                                                 "islocal", true  
    6.                        ));  


    方法4:使用协程

    [csharp] view plain copy
     
    1. StartCoroutine(MoveToPosition());  

    [csharp] view plain copy
     
    1. IEnumerator MoveToPosition()  
    2.     {  
    3.         GameObject m_UIbgCamera = GameObject.Find("UI/FengMian/UIbgCamera");  
    4.         while (m_UIbgCamera.transform.localPosition != new Vector3(-5, -3, 50))  
    5.         {  
    6.             m_UIbgCamera.transform.localPosition = Vector3.MoveTowards(m_UIbgCamera.transform.localPosition, new Vector3(-20, -3, 50), 10 * Time.deltaTime);  
    7.             yield return 0;  
    8.         }  
    9.     }  


     朝某个方向移动

            
    //方案一
    transform.Translate(Vector2.left * speed * Time.deltaTime * moveDir);
    
    //方案二
    Vector3 targetVelocity = new Vector2(-moveDir * speed, body2D.velocity.y);
    body2D.velocity = Vector3.SmoothDamp(body2D.velocity, targetVelocity, ref curSpeed, 0.1f);
     
  • 相关阅读:
    【原创】贴片电容的测量方法。。。这是我从自己QQ空间转过来的,本人实操!
    CentOS6.4安装Apache+MySQL+PHP
    第一次在博客园写博客。。。新人
    C# 简单生成双色球代码
    从客户端中检测到有潜在危险的 Request.Form 值 方法
    经典实例
    js鼠标键禁用功能
    逻辑思维题
    C#运算符笔记
    C#基础
  • 原文地址:https://www.cnblogs.com/sanyejun/p/7683641.html
Copyright © 2011-2022 走看看