I have a script which rotates the 3D model in world space using touch input. Right now, what is happening is that the model rotates from touching anywhere on the mobile screen. What i want to do is to rotate the 3D model by only touching its surface (3D model itself) and not by touching elsewhere on the mobile screen. Help me please, this is the Rotate script that i have applied on the 3D model:
void Update()
{
Debug.Log("Inside function Update");
HandleRotationInput();
}
void HandleRotationInput()
{
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved && !IsPointerOverUIObject(Input.GetTouch(0).fingerId) && !isZooming)
{
float rotationX = Input.GetTouch(0).deltaPosition.y * rotateSpeed * Time.deltaTime;
float rotationY = -Input.GetTouch(0).deltaPosition.x * rotateSpeed * Time.deltaTime;
// Rotate around the world axes instead of local axes
transform.Rotate(Vector3.right, rotationX, Space.World);
transform.Rotate(Vector3.up, rotationY, Space.World);
}
else if (Input.GetMouseButton(0) && !IsPointerOverUIObject() && !isZooming)
{
float rotationX = Input.GetAxis("Mouse Y") * rotateSpeed;
float rotationY = -Input.GetAxis("Mouse X") * rotateSpeed;
// Rotate around the world axes instead of local axes
transform.Rotate(Vector3.right, rotationX, Space.World);
transform.Rotate(Vector3.up, rotationY, Space.World);
}
}
// Check if the touch is over a UI object
bool IsPointerOverUIObject(int fingerId = -1)
{
PointerEventData eventData = new PointerEventData(EventSystem.current);
eventData.position = (fingerId == -1) ? Input.mousePosition : Input.GetTouch(fingerId).position;
// Raycast using the EventSystem to check if the touch is over a UI object
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, results);
return results.Count > 0;
}
Note: the hierarchy of 3D model is like this:
camera object-> game object (Rotate script is applied on this game object) → actual 3D model. Kindly someone help me, i am a beginner…