一、通過修改位置來實現(xiàn)移動
利用修改Transform組件的position的兩種常用方法。
- 使用Translate()函數(shù)
/*物體將向x方向移動1.5單位*/
transform.Translate(1.5f,0,0);
- 直接指定新的位置
/*將物體放在(1.5f,0,0)的位置上*/
transform.position = new Vector3(1.5f,0,0);
將上述兩種方法在 void Update()實現(xiàn)每一幀物體向x方向移動1.5個單位,具體代碼如下:
void Update()
{
transform.Translate(1.5f,0,0);
//或
transform.position += new Vector3(1.5f,0,0);
}
注意:此處1.5為啥要寫1.5f,根據(jù)C#的語法規(guī)定,直接寫1.5會被認(rèn)為double類型的數(shù),而這里需要flaot類型數(shù),三個數(shù)據(jù)只要其中一個帶有f就行。
由于電腦無法保證穩(wěn)定的幀率,會出現(xiàn)幀率高,物體移動就快,幀率低,物體移動就慢。而在游戲開發(fā)大部分情況中,我們應(yīng)該確?!泵棵胍苿油瑯拥木嚯x”,因此,我們應(yīng)該做如下修改。
void Update()
{
transform.Translate(1.5f*Time.deltaTime,0,0);
//或
transform.position += new Vector3(1.5f*Time.deltaTime,0,0);
}
此處*Time.deltatime是確保每秒移動同樣的距離。
Time.deltatime 是兩幀之間的間隔,如幀率為60幀/秒,則Time.deltatime =0.0167秒,或者幀率為10幀/秒,則Time.delatatime = 0.1秒。所以Time.delatatime是一個隨幀率改變而改變的數(shù)值,確保物體每秒移動的數(shù)值是一樣的。
ps:Time.deltatime是一個很小的數(shù)值,因此與他相乘的數(shù)值應(yīng)該大些。
二、通過物理系統(tǒng)實現(xiàn)位移
適用于對已經(jīng)掛載剛體組件的物體
以下是常用的兩種方法
- 利用AddForce()對物體施加力改變位置
public Rigidbody rb;
void Update()
{
rb.AddForce(100,0,0);
}
注意:一定要給物體掛載剛體,物體才會收到力的作用
此處AddForce()的參數(shù)為Vector3類型,該參數(shù)用一個向量表示力,且符合牛頓力學(xué)
這里表示每幀(時間)對物體軸方向施加100N的力,根據(jù)牛頓力學(xué),力至少持續(xù)一點時間才會引起物體速度的變化。所以此處必有位移,具體的位移,由你施加的力跟每幀的時間有關(guān)。在游戲開發(fā)中,我們只需要一邊測試一邊修改,確保一個合適數(shù)值即可。
- 直接修改物體的速度
此處表示物體在X軸以10m/s的速度移動,y軸跟z軸方向速度保持不變。
此方法相比上一個方法能讓物體直接跳過加速度引起速度變化的步驟,使物體能夠勻速運動。
注意:物理系統(tǒng)對于時間是非常敏感的。
舉個例子:我們需要子彈0.1秒后擊中目標(biāo),當(dāng)更新頻率不一定,子彈0.3秒才擊中目標(biāo),這不是我們想要的。
當(dāng)設(shè)備運行不流暢的時候,幀率下降,Time.deltatime會變大,不在適用。所以我們應(yīng)該做如下修改,利用 FixedUpdate()函數(shù),可以保證穩(wěn)定的間隔。獲取兩段Update之間的時間間隔為Time.deltatime,而獲得兩次FixedUpdate之間的時間間隔為Time.fixedDeltaTime,一般Time.fixedDeltaTime是一個固定的值(默認(rèn)為0.0.2秒,可通過Edit——Project Setting——Time來修改)。
修改代碼如下:
public Rigidbody rb;
void FixedUpdate()
{
rb.AddForce(10*Time.fixedDeltaTime,0,0);
//或
rb.velocity = new Vector3(10*Time.fixedDeltaTime,rb.velocity .y,rb.velocity.z);
}
三、通過CharacterController組件
private void Start()
{
_playerCC = this.GetComponent<CharacterController>();
}
void FixedUpdate()
{
_playerCC.Move(new Vector3(1.5f*Time.deltaTime,0,0););
}
四、通過輸入控制物體移動
第一種方法
void Update()
{
float horizontal = Input.GetAxis("Horizontal");
float vetical = Input.GetAxis("Vetical");
transform.Translate(horizontal*speed*Time.deltaTime,vetical*speed*Time.deltaTime,0);
//或
transform.position += new Vector3(horizontal*speed*Time.deltaTime,vetical*speed*Time.deltaTime,0);
}
此處的speed為一個變量,我們可以通過修改speed來控制物體移動的距離。
假設(shè)我們定義 float speed =10;
即此處每幀最大的位移為1100.0167=0.167米。
第二種方法
public Rigidbody rb;
public float speed;
void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
float vetical = Input.GetAxis("Vetical");
rb.AddForce(horizontal*speed*Time.fixedDeltaTime,vetical*speed*Time .fixedDeltaTime,0);
//或添加速度
rb.velocity = new Vector3(horizontal*speed*Time.fixedDeltaTime,vetical*speed*Time.fixedDeltaTime,rb.velocity.z);
}
第三種方法文章來源:http://www.zghlxwxcb.cn/news/detail-786860.html
private void Start()
{
_playerCC = this.GetComponent<CharacterController>();
}
public void FixedUpdate()
{
Vector3 motionValue = Vector3.zero;
/*獲取鍵盤輸入*/
float h_InputValue = Input.GetAxis("Horizontal");//左右移動
float v_InputValue = Input.GetAxis("Vertical");//前后移動
motionValue += this.transform.forward * v_InputValue * moveSpeed * Time.fixedDeltaTime;//前后方向的位移
motionValue += this.transform.right * h_InputValue * moveSpeed * Time.fixedDeltaTime;//左右方向的位移
_playerCC.Move(motionValue);
}
通過上述的三種方法,我們就可以實現(xiàn),每當(dāng)按下相應(yīng)的鍵,物體就會往相應(yīng)的方向移動一定的距離。文章來源地址http://www.zghlxwxcb.cn/news/detail-786860.html
到了這里,關(guān)于【Unity入門】物體5種移動方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!