unity3d中控制物體移動方法有那些及區別


1. 利用GameObject的Translate,直接改變它的Transform,前提是需要你實現准備變換矩陣
2. 用MoveTo方法,你只要知道你的目標位置即可。
3. 用Math的Lerp方法計算位置分量的線性差值,以TimeDelta(或其倍數)為調節量,可以使移動變得很平滑。這個方法可與方法2結合使用。



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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM