How to make object rotate on android platform
Hello
I have found a few scripts already written and i have tried them but with no luck. So i decided to try and write my own with googles help ofcourse :) Somehow my rotation isn't working in the right directions and i have no idea why.Basically i want it to rotate in any direction that i swiped my screen in.Can anyone help me?If anything else is needed please let me know.
Here is my code:
using UnityEngine;
using System.Collections;
public class RotateBall : MonoBehaviour
{
public float speed = 0.2F;
void Update()
{
int nbTouches = Input.touchCount;
if (nbTouches > 0)
{
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
TouchPhase phase = touch.phase;
switch (phase)
{
case TouchPhase.Began:
break;
case TouchPhase.Moved:
transform.Rotate(new Vector3(touch.deltaPosition.y*speed, touch.deltaPosition.x * speed));
break;
case TouchPhase.Ended:
break;
}
}
}
}
}
Answer by cjdev · Feb 05, 2016 at 12:35 AM
Personally I had trouble with the touch phases and couldn't get them to work reliably. I ended up going with deltaPosition and modifying the rotation that way. It looked something like this (note that this is untested and the rotations are probably out of order):
for (int i = 0; i < Input.touchCount; i++)
{
transform.rotation *= Quaternion.Euler(
-Input.GetTouch(i).deltaPosition.y,
Input.GetTouch(i).deltaPosition.x, 0);
}
transform.rotation = Quaternion.Euler(
transform.rotation.eulerAngles.x,
transform.rotation.eulerAngles.y,0);
Answer by zoorrax · Apr 05, 2017 at 07:48 AM
thanks solarstorm... your code worked!!!!! transform.Rotate(new Vector3(touch.deltaPosition.y*speed, touch.deltaPosition.x * speed,0)); i just put a zero in z direction.