单击按钮旋转游戏对象

时间:2011-10-27 12:28:23

标签: unity3d rotation unityscript gameobject

我有一个对象和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;
    }
}

1 个答案:

答案 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 - 它应该是自上次OnGUI以来的时间,但我不确定。为避免出现问题,我将实际轮播放在Update中,并使用两个控件布尔值(rotLeftrotRight)与OnGUI进行通信。