2013-04-16 64 views
1

這裏對象的鉗位X位置是我在統一代碼3D團結3D

moveDirection = Vector3.forward + new Vector3(Input.acceleration.x * 0.3f, 0, 0); 

// transform.position.x = Mathf.Clamp(transform.position.x, -2.0f, 2.0f); 

transform.Translate(moveDirection * Time.deltaTime *9); 

目的向前移動。我想鉗住它的x位置。

transform.position.x = Mathf.Clamp(transform.position.x, -2.0f, 2.0f); 

這給了我

錯誤CS1612:不能修​​改`UnityEngine.Transform.position」的值類型返回值。考慮將該值存儲在一個臨時變量中

如何夾住我的物體?

回答

3

Unity在這方面非常愚蠢,所以需要一些解決方法。試試這個:

Vector3 tmpPos = transform.position; 
tmpPos.x = Mathf.Clamp(tmpPos.x, -2.0f, 2.0f); 
transform.position = tmpPos; 
+0

獲取錯誤CS0029:無法鍵入'浮法」隱式轉換爲'UnityEngine.Vector3 – Sona

+0

@Sona oups,忘了我編輯我的答案與修復的.X,所以它應該是確定現在 –

+0

謝謝,其工作正常。 – Sona