- Home /
Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag()
Hello guys I've been struggling in rotating a gameobject on y and z axis only with Y value clamped between (-90 & -120) & Z value between (0 & -10). I've tried many online solutions with eulerangles and rotation and still stuck. I want to rotate 3D model with drag so I thought that OnMouseDrag would work. Any help would be really appreciated. Thanks.
Answer by xxmariofer · May 29, 2019 at 08:55 AM
test this code, is not optimised but it should work :) ALSO take into account this, dont work with euler angles with negative values, since for unity -110 is 250 so you will have a lot of issues comparing values (you will probably need to use the positive values)
void Update()
{
if (Input.GetMouseButtonDown(0)) lastPosition = Input.mousePosition;
}
Vector2 lastPosition;
public float speed = 1f;
private void OnMouseDrag()
{
Debug.Log(transform.rotation.eulerAngles.y);
if (Input.mousePosition.x > lastPosition.x + 10)
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x,Mathf.Clamp( transform.rotation.eulerAngles.y + speed, 0, 30), transform.rotation.eulerAngles.z);
else if(Input.mousePosition.x < lastPosition.x - 10)
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 0, 30), transform.rotation.eulerAngles.z);
if (Input.mousePosition.y > lastPosition.y + 10)
transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x + speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
else if (Input.mousePosition.y < lastPosition.y - 10)
transform.rotation = Quaternion.Euler(Mathf.Clamp(transform.rotation.eulerAngles.x - speed, 0, 10), transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
}
Answer by OGNE · May 29, 2019 at 11:05 AM
I changed your code a little bit according to my values but there is still some error in Z axis. Its quickly snaps between the values used in code but tha's not the case with Y axis. The y axis is working perfectly. My 3D model is set as (0,270,360) and it is supposed to go to (0,230,340). Here is the code I changed
Vector2 lastPosition;
public float speed = 1f;
private void OnMouseDrag()
{
//Debug.Log(transform.rotation.eulerAngles.y);
if (Input.mousePosition.x < lastPosition.x + 10)
{
print("condition 1");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y + speed, 230, 270), transform.rotation.eulerAngles.z);
}
else if (Input.mousePosition.x > lastPosition.x - 10)
{
print("condition 2");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, Mathf.Clamp(transform.rotation.eulerAngles.y - speed, 230, 270), transform.rotation.eulerAngles.z);
}
if (Input.mousePosition.y > lastPosition.y + 10)
{
print("condition 3");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 340, 360));//org
}
else if (Input.mousePosition.y < lastPosition.y - 10)
{
print("condition 4");
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Clamp(transform.rotation.eulerAngles.z - speed, 360, 340));//org
}
}
void OnMouseUp()
{
transform.rotation = Quaternion.Euler(0, 270, 360);
}
it is not working because you are doing "- speed" in both if/elses you need to change it to + speed
only change to + speed in the condition3, and change in the condition 4 the 340 to be before the 360 (since the first value is the $$anonymous$$ and the second the max)
Your answer
Follow this Question
Related Questions
My Obstacle Rotations Are Not Correct 0 Answers
Code to rotate around a local axis until local Y is 0? 0 Answers
Why Is Rotation Different For RectTransform? 0 Answers
How to set a single-axis rotation and not change the others axes 1 Answer
how to change player rotation in topdown view game 2 Answers