【轉】unity 移動物體到指定位置的四種方法

來源:互聯網
上載者:User

標籤:協程   towards   tle   alt   cal   atime   article   csdn   指定位置   

http://blog.csdn.net/lcy0221/article/details/44040739

方法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.     }  

【轉】unity 移動物體到指定位置的四種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.