- Home /
How to limit object's Y rotation to 45 degree increments
I'm looking to make buildable walls in my game, which I would like to be able to rotate based on the direction the player is looking. However, I want to limit it's Y rotation to 45 degree increments (0, 45, 90, etc), however so far my attempts result in the rotation on the Y axis being stuck at 0. Here's my code, if it's any help:
tempRot.y=Mathf.Round(tempRot.y/45)*45;
Any ideas or suggestions on a different way to do this?
What's tempRot and where is that piece of code called? If it is supposed to be the rotation resulting from a mousedrag, keep in $$anonymous$$d that by rounding you are ALWAYS rounding it to 0, unless the movement of the mouse is very quick. I'm assu$$anonymous$$g you're having some sort of display of where the wall will be placed and at what angle, right? In that case you'll most likely want a separate variable for keeping track of the input rotation and the rotation of your 'gizmo'. Your other lines for tempRot can probably stay the same, but the rotation of that gizmo should actually be the code you posted here.
tempRot is a Quaternion that starts off equal to the camera's transform.rotation variable. I change the x/z to 0 (so you can't make an upside wall, for instance), and change the y in the code seen above.
Here's the whole bit of code, it's part of FixedUpdate():\
Quaternion tempRot = camera.transform.rotation;
tempRot.x=0;
tempRot.z=0;
tempRot.y=$$anonymous$$athf.Round(tempRot.y/45)*45;
int distance=3;
if(buildType==1)
{
Vector3 newpos = camera.transform.position + camera.transform.forward*distance;
firstBuildablePrefabBuildPos.transform.rotation=tempRot;
Ray ray = new Ray(newpos, Vector3.down);
RaycastHit hit;
Physics.Raycast(ray, out hit, 2.5f);
{
firstBuildablePrefabBuildPos.transform.position = new Vector3($$anonymous$$athf.RoundToInt(newpos.x/2)*2, hit.point.y+1, $$anonymous$$athf.RoundToInt(newpos.z/2)*2);
}
}
firstBuildablePrefabBuildPos (long name, I know) is the preview gizmo.
Answer by robertbu · Aug 13, 2014 at 08:19 PM
The individual components of a Quaternion (x, y, z, w) are not angles. They are values between -1 and 1. For the description of how you have things setup, you can use tempRot.eulerAngles.y instead:
Vector3 vv = tempRot.eulerAngles;
vv.y = Mathf.Round(vv.y/45.0f) * 45.0f;
tempRot.eulerAngles = vv;
As long as you are only rotating on the 'y', then you will be okay, but for more general rotations, it is 'dangerous' to read from eulerAngles. The representation, while accurate, can change. For example (180,0,0) might become (0,180,180)...the same physical rotation represented differently.
Edit: How you are setting up tempRot is also an issue. Do this instead:
Vectror3 v = camera.transform.forward;
v.y = 0.0f;
Quaternion tempRot = Quaternion.LookRotation(v);
Answer by IbQuezada · Aug 13, 2014 at 09:03 PM
Try using modulus, e.g.
float angle = 90;
if (angle % 45 == 0) { // if the result of angle divided by 45 is 0
// do the rotation
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613160537im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unlock rotation from script 1 Answer
Flip over an object (smooth transition) 3 Answers
Manually computing rotation constrain limits 0 Answers
Unity 2d - Rigidbody rotation constraints? 4 Answers
Rigidbody constraints in local space? 7 Answers