2016-12-24 37 views
0

我能夠在使用該代碼的多個方向旋轉立方體旋轉立方體:團結在多個方向

public float speed = 100f; 
public Animation anim; 
Animator animator; 

public Vector3 RotateAmount; 

bool running = false; 

void Start() 
{ 
    animator = GetComponent<Animator>(); 
} 

void Update() 
{ 
    if (Input.GetKey("up")) 
     transform.Rotate(Vector3.right, speed); 
    if (Input.GetKey("left")) 
     transform.Rotate(Vector3.up, speed); 
    if (Input.GetKey("right")) 
     transform.Rotate(Vector3.down, speed); 
    if (Input.GetKey("down")) 
     transform.Rotate(Vector3.left, speed); 
} 

現在,我想要的是不管面對哪個在前面旋轉的立方體。但上面的代碼不會這樣做。如果我按一下然後它會右轉,但是當我按右時它向左旋轉。這是我想要避免的。有人能幫助我弄清楚邏輯或任何想法嗎?

初學者與團結請。

+0

我很長一段時間沒有團結,我幾乎不明白你在說什麼......但也許這可以幫助:http://stackoverflow.com/questions/28648071/rotate-object-in-unity-3d – deadManN

回答

1

您應該將speedTime.deltaTime相乘,否則您的轉速將取決於當前幀速率。

您可能會在本地空間中添加旋轉,從而將所有內容混淆。

RotateScript.cs

using UnityEngine; 

public class RotateScript : MonoBehaviour 
{ 

float speed = 100f; 

// Use this for initialization 
void Start() 
{ 

} 

// Update is called once per frame 
void Update() 
{ 
    if (Input.GetKey("up")) 
    { 
     transform.RotateAround(this.transform.position, new Vector3(1, 0, 0), speed * Time.deltaTime); 
    } 
    if (Input.GetKey("down")) 
    { 
     transform.RotateAround(this.transform.position, new Vector3(-1, 0, 0), speed * Time.deltaTime); 
    } 
    if (Input.GetKey("left")) 
    { 
     transform.RotateAround(this.transform.position, new Vector3(0, 1, 0), speed * Time.deltaTime); 
    } 
    if (Input.GetKey("right")) 
    { 
     transform.RotateAround(this.transform.position, new Vector3(0, -1, 0), speed * Time.deltaTime); 
    } 
} 
} 

此代碼是繞着它的當前位置的立方體世界this.transform.position(如果你使用不同的模型確保這點是對象的中心,否則旋轉看起來很奇怪)。

對於未以原點爲中心建模的對象,您可以嘗試使用transform.renderer.bounds.center來獲取中心。

+0

我不知道爲什麼,但我用你的替換我的更新代碼,整個場景只要我玩它並按任何方向鍵就會凍結。 – xhammer

+1

您是否在新項目中嘗試過我的代碼?你使用的是什麼版本的團結? – Skynet

+0

對不起,只是使用GetComponent ().bounds.center,它似乎工作。非常感謝,它現在完美。向右移動而不是向上或在幾次旋轉之後。也感謝代碼。還有一件事。我如何確保一張臉變成另一張臉?這樣我就不會有旋轉的頂點,它會平滑(試圖做一個謎題求解器)。 – xhammer