2011-10-27 50 views
1

我有一个对象和2个GUI纹理按钮。当我按下左边的按钮时,我要将对象旋转到左边,同时按下另一个。通过单击按钮来旋转游戏对象

任何想法?

我有一个脚本,当我拖动我的对象时起作用。我将发布的重要组成部分:

function Start() 
{ 
    var angles = transform.eulerAngles; 
    x = angles.y;  

    // Make the rigid body not change rotation 
    if (rigidbody) 
    rigidbody.freezeRotation = true;  
} 

function LateUpdate() 
{ 
    if (isMouseOverGuiTexture()) return; 

    if (target && Input.GetMouseButton(0)) 
    { 
     //0.1 represents the sensitivity of the mouse 
     x += Input.GetAxis("Mouse X") * xSpeed *0.1; //x rotation 
     //y -= Input.GetAxis("Mouse Y") * ySpeed *0.1; //y rotation 
     //y = ClampAngle(y, yMinLimit, yMaxLimit);     
     var rotation = Quaternion.Euler(y, x, 0); 
     var position = rotation * Vector3(0.900528, 8.829305, -distance+0.49548)+ target.position; 

     transform.rotation = rotation; 
     transform.position = position; 
    } 
} 
+0

什么API?看起来像JavaScript? – trojanfoe

+0

统一3d和代码的JavaScript。 –

+0

我编辑了我的问题,并添加了答案。对不起,如果我做错了什么。 –

回答

0

(问题回答在评论见Question with no answers, but issue solved in the comments (or extended in chat)

的OP写道:

我解决它通过使用下列内容:

var imageLeft: Texture2D; // drag the left button image here 
var imageRight: Texture2D; // right button image 
var speed: float = 60; // rotate speed in degrees per second 
private var rotLeft = false; // object rotates left if true 
private var rotRight = false; // object rotates right if true; 

function OnGUI() 
{ 
    rotLeft = GUI.RepeatButton(Rect(10,10,200,200), imageLeft); 
    rotRight = GUI.RepeatButton(Rect(230,10,200,200), imageRight); 
} 

function Update() 
{  
    if (rotLeft) transform.Rotate(0, -speed*Time.deltaTime, 0); 
    if (rotRight) transform.Rotate(0, speed*Time.deltaTime, 0); 
} 

我不知道Time.deltaTime假设在OnGUI的哪个值 - 它肩d是自上次OnGUI以来的时间,但我不确定。为了避免问题,我将实际轮转放在Update中,并使用两个控制布尔(rotLeftrotRight)与OnGUI进行通信。