所以在Unity3D中,我有一個由玩家控制的立方體(使用箭頭)。玩家可以隨意使用立方體去任何他想去的地方。問題是,當我將剛體加到立方體上時,它不再那麼光滑了,不知怎的,最後它只是小故障。試圖玩torque
,但沒有幫助,或者我不明白如何正確實施它。這是我用於多維數據集的代碼(它是用JavaScript編寫的)。任何幫助將不勝感激,即使你不知道的JavaScript仍然重要的想法。謝謝。代碼:在Unity3D中滾動剛體立方體
private var ismoving : boolean = false;
private var startY : float = 0;
var cubeSpeed : float;
var cubeSize : float;
var jumpHeight = 3.0;
function Update()
{
if (Input.GetKeyDown("up") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2 , cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.right, 90.0f,cubeSpeed));
}
if (Input.GetKeyDown("down") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(0, -cubeSize/2, -cubeSize/2);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.right, 90.0f,cubeSpeed));
}
if (Input.GetKeyDown("left") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(-cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, Vector3.forward, 90.0,cubeSpeed));
}
if (Input.GetKeyDown("right") && ismoving == false)
{
ismoving = true;
transform.Find("targetpoint").Translate(cubeSize/2, -cubeSize/2, 0);
StartCoroutine(DoRoll(transform.Find("targetpoint").position, -Vector3.forward, 90.0f,cubeSpeed));
}
if (Input.GetKeyDown("space") && ismoving == false)
{
GetComponent.<Rigidbody>().AddForce(new Vector3(0, jumpHeight, 0), ForceMode.Impulse);
}
}
function DoRoll (aPoint, aAxis, aAngle, aDuration) {
var tSteps = Mathf.Ceil(aDuration * 30.0);
var tAngle = aAngle/tSteps;
var pos : Vector3;
for (var i = 1; i <= tSteps; i++)
{
transform.RotateAround (aPoint, aAxis, tAngle);
yield WaitForSeconds(0.0033333);
}
transform.Find("targetpoint").position = transform.position;
pos = transform.position;
pos.y = startY;
transform.position = pos;
var vec = transform.eulerAngles;
vec.x = Mathf.Round(vec.x/90) * 90;
vec.y = Mathf.Round(vec.y/90) * 90;
vec.z = Mathf.Round(vec.z/90) * 90;
transform.eulerAngles = vec;
ismoving = false;
}
function FixedUpdate() {
}
太多的事情錯在你的代碼。 1.使用Javascript而不是C#。 2.不緩存轉換(每次使用.Find)。 3.不緩存Rigibody。請學習C#。這並不難。 – Programmer
AndroidFreak,Unity腳本不再可用。你必須改用c#。 – Fattie