Tarek-Dev
Tarek-Dev

Reputation: 170

Change controls from mouse to touch in Unity C#

I am trying to create a spin wheel that follows the direction of the swipe. Currently, it works fine with mouse controls but i failed to make this script work with touch controls. Any idea how to do that?

Script:

void Update () 
{
  if (Input.GetMouseButtonDown(0))
  {
      f_difX = 0.0f;
  }
  else if (Input.GetMouseButton(0))
  {
      f_difX = Mathf.Abs(f_lastX - Input.GetAxis ("Mouse X"));

      if (f_lastX < Input.GetAxis ("Mouse X"))
      {
          i_direction = -1;
          transform.Rotate(Vector3.up, -f_difX);
      }

      if (f_lastX > Input.GetAxis ("Mouse X"))
      {
          i_direction = 1;
          transform.Rotate(Vector3.up, f_difX);
      }

      f_lastX = -Input.GetAxis ("Mouse X");
  }
  else 
  {
      if (f_difX > 0.5f) f_difX -= 0.05f;
      if (f_difX < 0.5f) f_difX += 0.05f;

      transform.Rotate(Vector3.up, f_difX * i_direction);
  }
}

Thanks.

Upvotes: 1

Views: 2350

Answers (1)

mbnx
mbnx

Reputation: 942

Take a look at this thread: http://answers.unity3d.com/questions/504707/replace-input-get-axis-horizontal-with-touch-input.html, first answer.

The link provides a code snippet showing how to use the calculated position delta to replace Input.GetAxis.

Upvotes: 2

Related Questions