0

I was wondering if there's a way to add (in my case) 120 degrees everytime I push 'ButtonA', and subtract 120 degrees everytime I push 'ButtonB', from the Z-axis rotation of a 2d sprite (prefab).

This is the code I'm using at the moment, but it only rotates once left, and once right:

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");
        }
    }
}

Thanks in advance!

Note: Please use unityscript if you can, I'm pretty new to coding and unityscript is all I know so far.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Bartvb98
  • 9
  • 2
  • 7

2 Answers2

0

From the online documentation it is shown that the signature of the function is

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

that means the second parameter is the new rotation of the object not the rotation offset

you should use something of the sort

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");
    }
}
}

note the second parameter is transform.rotation + Quaternion.Euler(0,0,120) (current rotation + offset to the right)

I'm not an expert on unity engine (I only started toying with the free version yesterday, literally)

workoverflow
  • 578
  • 5
  • 16
  • Thanks for your answer @workoverflow, but I'm getting this error: BCE0051: Operator '+' cannot be used with a left hand side of type 'UnityEngine.Quaternion' and a right hand side of type 'UnityEngine.Quaternion'. – Bartvb98 Sep 11 '14 at 13:44
0

Try this one

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");
    }
}
}

If this not works try with Gameobject. I didn't check this

Rasa Mohamed
  • 882
  • 1
  • 6
  • 14
  • Thanks for your answer @Rasa Mohamed , but the problem with this script is, is that the rotation isn't smoothly. It snaps from point A to point B in one frame. I want it so that the rotation is visible. – Bartvb98 Sep 11 '14 at 13:47