Question by
mesmes123 · Oct 26, 2021 at 02:43 PM ·
rotationmousedragmouseclickonmousedown
OnMouseDrag() stopped working after few clicks
I am facing a problem. After a few mouse clicks, the function stops working... I am using the OnMouseDrag() function for rotating an object in the center of my scene. The script is attached to the gameObject, the gameObject has a BoxCollider. What is the problem? Code:
public float PCRotationSpeed = 10f; public float MobileRotationSpeed = 0.4f; public Camera cam;
void OnMouseDrag()
{
Debug.Log("OnMouseDrag() - START");
float rotX = Input.GetAxis("Mouse X") * PCRotationSpeed;
float rotY = Input.GetAxis("Mouse Y") * PCRotationSpeed;
Vector3 right = Vector3.Cross(cam.transform.up, transform.position - cam.transform.position);
Vector3 up = Vector3.Cross(transform.position - cam.transform.position, right);
transform.rotation = Quaternion.AngleAxis(-rotX, up) * transform.rotation;
transform.rotation = Quaternion.AngleAxis(rotY, right) * transform.rotation;
Debug.Log("OnMouseDrag() - END");
}
void Update()
{
// if we are using mobile we need to detect the user touch input, if pc we do not need
foreach (Touch touch in Input.touches)
{
Debug.Log("Touching at: " + touch.position);
Ray camRay = cam.ScreenPointToRay(touch.position);
//make sure we are touching the object and not around it.
RaycastHit raycastHit;
if (Physics.Raycast(camRay, out raycastHit, 1))
{
if (touch.phase == TouchPhase.Began)
{
Debug.Log("Touch phase began at: " + touch.position);
}
else if (touch.phase == TouchPhase.Moved)
{
Debug.Log("Touch phase Moved");
transform.Rotate(touch.deltaPosition.y * MobileRotationSpeed,
-touch.deltaPosition.x * MobileRotationSpeed, 0, Space.World);
}
else if (touch.phase == TouchPhase.Ended)
{
Debug.Log("Touch phase Ended");
}
}
}
}
Comment