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;
    }
}
4

1 回答 1

0

(评论中回答的问题。请参阅没有答案的问题,但问题已在评论中解决(或在聊天中扩展)

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.

于 2015-01-26T15:11:50.700 回答