Limit/ clamp rotation

35 Views Asked by At

Im trying to limit the rotation as the title says between all axises idk how to realy explain this so i will show you the code

i think its worth mentioning that the object (chessboard) im trying to rotate is roatated 90 in x axis so thats where the problem drivens.

    void OnMouseDrag()
    {
        // Get mouse input
        float XaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
        float YaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;

        // Adjust rotation to account for initial X rotation of 90 degrees
        Quaternion initialRotationOffset = Quaternion.Euler(90, 0, 0);

        // Rotate the object
        rigid.transform.Rotate(initialRotationOffset * Vector3.back, XaxisRotation);
        rigid.transform.Rotate(Vector3.right, YaxisRotation);

        // Apply rotation limits if enabled
        if (USELIMITS)
        {
            Vector3 currentRotation = rigid.transform.eulerAngles;

            // Clamp rotations within specified limits
            float clampedXRotation = Mathf.Clamp(currentRotation.x, -xRotLimit, xRotLimit);
            float clampedYRotation = Mathf.Clamp(currentRotation.y, -yRotLimit, yRotLimit);

            // Apply clamped rotations
            rigid.transform.eulerAngles = new Vector3(clampedXRotation, clampedYRotation, 0);
        }
    }

Ive tried many ways tbh

        if (USELIMITS)
        {
            //X
            if (rigid.transform.rotation.x > xRotLimit -90)
            {
                rigid.transform.rotation = new Quaternion(xRotLimit-90, 0, transform.rotation.z, transform.rotation.w);
            }
            if (rigid.transform.rotation.x < -xRotLimit-90)
            {
                rigid.transform.rotation = new Quaternion(-xRotLimit-90, 0, transform.rotation.z, transform.rotation.w); 
            }
            //Z
            if (rigid.transform.rotation.z > zRotLimit)
            {
                rigid.transform.rotation = new Quaternion(transform.rotation.x, 0, zRotLimit, transform.rotation.w);
            }
            if (rigid.transform.rotation.z < -zRotLimit)
            {
                rigid.transform.rotation = new Quaternion(transform.rotation.x, 0, -zRotLimit, transform.rotation.w);
            }
        }
    }
        if (USELIMITS)
        {
            // Get the current tilt rotation
            float currentTilt = rigid.transform.localEulerAngles.x;

            // Apply tilt limits
            if (currentTilt > tiltLimit && currentTilt < 180)
            {
                rigid.transform.localEulerAngles = new Vector3(tiltLimit, rigid.transform.localEulerAngles.y, rigid.transform.localEulerAngles.z);
            }
            else if (currentTilt < 360 - tiltLimit && currentTilt >= 180)
            {
                rigid.transform.localEulerAngles = new Vector3(360 - tiltLimit, rigid.transform.localEulerAngles.y, rigid.transform.localEulerAngles.z);
            }
        }
 None of those seem to work the object always lock in the same place despite puting different variable values every time.

also in the image i kind ilsutraded what i want to do I wanna tilt the chess board in all direction using a mouse but I cant get my head around how to clamp it so yeah thats basically it

0

There are 0 best solutions below