0

我想知道是否有办法从二维精灵(预制件)的Z 轴旋转中每次按下“ButtonA”时添加(在我的情况下)120 度,并在每次按下“ButtonB”时减去 120 度。

这是我目前正在使用的代码,但它只向左旋转一次,向右旋转一次:

function TouchOnScreen ()
{
    if (Input.touchCount > 0)
    {
        var touch = Input.touches[0];
        if (touch.position.x < Screen.width/2)
        {
            var RSpeed = 10.0f
            transform.rotation = Quaternion.Lerp ( transform.rotation,Quaternion.Euler(0,0,120), Time.deltaTime*RSpeed);
            Debug.Log("RotateRight");
        }
        else if (touch.position.x > Screen.width/2)
        {
            var LSpeed = 10.0f
            transform.rotation = Quaternion.Lerp ( transform.rotation,Quaternion.Euler(0,0,-120), Time.deltaTime*LSpeed);
            Debug.Log("RotateLeft");
        }
    }
}

提前致谢!

注意:如果可以的话,请使用unityscript,我对编码很陌生,到目前为止我只知道unityscript。

4

2 回答 2

0

试试这个

function TouchOnScreen ()
{
if (Input.touchCount > 0)
{
    var touch = Input.touches[0];
    if (touch.position.x < Screen.width/2)
    {
        var RSpeed = 10.0f
        transform.Rotate(0,0,120);
        Debug.Log("RotateRight");
    }
    else if (touch.position.x > Screen.width/2)
    {
        var LSpeed = 10.0f
        transform.Rotate(0,0,-120);
        Debug.Log("RotateLeft");
    }
}
}

如果这不起作用,请尝试使用 Gameobject。我没有检查这个

于 2014-09-11T06:27:51.120 回答
0

在线文档中可以看出,函数的签名是

static function Lerp(from: Quaternion, to: Quaternion, t: float): Quaternion; 

这意味着第二个参数是对象的新旋转而不是旋转偏移

你应该使用类似的东西

function TouchOnScreen ()
{
if (Input.touchCount > 0)
{
    var touch = Input.touches[0];
    if (touch.position.x < Screen.width/2)
    {
        var RSpeed = 10.0f
        transform.rotation = Quaternion.Lerp ( transform.rotation,transform.rotation + Quaternion.Euler(0,0,120), Time.deltaTime*RSpeed);
        Debug.Log("RotateRight");
    }
    else if (touch.position.x > Screen.width/2)
    {
        var LSpeed = 10.0f
        transform.rotation = Quaternion.Lerp ( transform.rotation,transform.rotation + Quaternion.Euler(0,0,-120), Time.deltaTime*LSpeed);
        Debug.Log("RotateLeft");
    }
}
}

注意第二个参数是transform.rotation + Quaternion.Euler(0,0,120)(当前旋转+向右偏移)

我不是统一引擎方面的专家(从字面上看,我昨天才开始玩免费版本)

于 2014-09-10T23:20:30.283 回答