My Rotate Codes Doesn't Work With Touch

Hello everyone,

I am trying to rotate my object with these codes, actually they work with key controls but they don’t work with touch or mouse functions. How should i modify these to make them work with touch or mouse functions either?

public float smooth = 2.0F;
public float tiltAngle = 30.0F;

bool rotateLeft = false;

bool rotateRight = false;

voidUpdate() {
  if (Input.touchCount > 0)
{

Touchtouch = Input.GetTouch (0);

if (touch.position.x < Screen.width/2)
{
rotateLeft = true;
}

if (touch.position.x > Screen.width/2)
{
rotateRight = true;
}

if(Input.touchCount == 0)
{
if(rotateLeft == true)
rotateLeft = false;


if(rotateRight == true)
rotateRight = false;
}

Debug.Log("Rotate LEFT " + rotateLeft);

Debug.Log("Rotate RIGHT " + rotateRight);

}

if(rotateLeft == true)
{
floattiltAroundZ = -Input.GetAxis("Horizontal") * tiltAngle;
Quaterniontarget = Quaternion.Euler(0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}else
{
floattiltAroundZ = -Input.GetAxis("Horizontal") * tiltAngle;
Quaterniontarget = Quaternion.Euler(0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}

if(rotateRight == true)
{
floattiltAroundZ = Input.GetAxis("Horizontal") * -tiltAngle;
Quaterniontarget = Quaternion.Euler(0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
else{
floattiltAroundZ = Input.GetAxis("Horizontal") * -tiltAngle;
Quaterniontarget = Quaternion.Euler(0, 0, tiltAroundZ);
transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
}
}
}

Input.GetAxis() would only return a value if there is something to report for that axis. Touch events don’t generate axis values, so that’s why it doesn’t work.

@blizzy How can i get it work? What should i add instaed of GetAxis?

You seem to use GetAxis() to basically calculate the angle to rotate by. That doesn’t work for digital (on/off) events like a touch. For digital events, you may want to use a fixed value.

I’ve deleted GetAxis and now everything works well. Thanks for your help!