Hi everyone.
I try to rotate a gameobject with the touch input and try to clamp the x rotation but the dont work how i desire, if i drag from the top to the button it rotate well, but if i drag from the button to the top (when reach the 0 valeu in rotation x in inspector) it start flickering and it returns to a rotation over +40.
this is the code
public class Visualizzatore3D : MonoBehaviour
{
public GameObject objectToRotate;
private Vector3 _touchPosition;
private Quaternion rotazione = Quaternion.identity;
private float _scaleFactor = 1f;
float initialXRotation;
private void Start()
{
initialXRotation = objectToRotate.transform.localRotation.eulerAngles.x;
}
private void Update()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
float rotationSpeed = 0.2f;
float rotationX = -touch.deltaPosition.y * rotationSpeed;
float rotationY = -touch.deltaPosition.x * rotationSpeed;
Quaternion newRotation = Quaternion.Euler(rotationX, rotationY, 0);
newRotation = objectToRotate.transform.localRotation * newRotation;
float clampedAngle = Mathf.Clamp(newRotation.eulerAngles.x, -45f, 45f);
//Vector3 clampedEuler = new Vector3(clampedAngle, newRotation.eulerAngles.y, 0);
objectToRotate.transform.localRotation = Quaternion.Euler(clampedAngle,newRotation.eulerAngles.y,0);
}
}
}
Thanks to everyone!