单击按钮旋转游戏对象

问题描述 投票:1回答: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;
    }
}
unity3d rotation unityscript gameobject
1个回答
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.deltaTimeOnGUI内假设的值 - 它应该是自上次OnGUI以来的时间,但我不确定。为了避免出现问题,我将实际旋转放在Update并使用两个控制布尔(rotLeftrotRight)与OnGUI进行通信。

© www.soinside.com 2019 - 2024. All rights reserved.