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?

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);

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
}