Hello, i am trying 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.
I found a solution for whomever might need it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinWheelScript : MonoBehaviour {
float pointerY;
float f_lastX = 0.0f;
float f_difX = 0.5f;
float f_steps = 0.0f;
int i_direction = 1;
void Start()
{
pointerY = Input.GetAxis("Mouse Y");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
f_difX = 0.0f;
}
else if (Input.touchCount > 0)
{
pointerY = Input.touches[0].deltaPosition.y;
f_difX = Mathf.Abs(f_lastX - pointerY);
var touch = Input.GetTouch(0);
if (touch.position.x > Screen.width / 2)
{
// Right
if (f_lastX < Input.GetAxis("Mouse Y"))
{
i_direction = 1;
transform.Rotate(Vector3.forward, f_difX * Time.deltaTime);
}
if (f_lastX > Input.GetAxis("Mouse Y"))
{
i_direction = -1;
transform.Rotate(Vector3.forward, -f_difX * Time.deltaTime);
}
}
else if (touch.position.x < Screen.width / 2)
{
// Left
if (f_lastX < Input.GetAxis("Mouse Y"))
{
i_direction = -1;
transform.Rotate(Vector3.forward, -f_difX * Time.deltaTime);
}
if (f_lastX > Input.GetAxis("Mouse Y"))
{
i_direction = 1;
transform.Rotate(Vector3.forward, f_difX * Time.deltaTime);
}
}
f_lastX = -pointerY;
f_difX = 500f;
Debug.Log(f_difX);
}
else
{
if (f_difX > 0.5f) f_difX -= 1f;
if (f_difX < 0.5f) f_difX += 1f;
transform.Rotate(Vector3.forward, f_difX * i_direction * Time.smoothDeltaTime);
}
}
}
Currently, the object will spin based on a swipe along the Y axis, you can simple change it by replacing the Y’s with X’s. Also, i’ve modified the speeds to fit my liking. Hope this helps others.
Cheers!