# Snap object rotation

How can I snap the rotation of an object to 90 degree angle? I want the object to face the player, but in 90 degree increments. For example, if the rotation of the object is 45, it should instead point to 90, and if the rotation is 20, is should point to 0. The reason I want to do this is because I’m making a fortnite like building system. I’ve gotten the grid system down, and it successfully snaps to a invisible grid, but now I need the building object to point toward the player, so it doesn’t always point in one direction. Here is the code I have so far (I found it off of This link GameObject wall = PhotonNetwork.Instantiate(Path.Combine(“PhotonPrefabs”, “Wall”), buildingEnd.position, Quaternion.identity) as GameObject; wall.transform.position = gameManager.grid.GetNearestPointOnGrid(wall.transform.position); Vector3 lookAtTarget = transform.eulerAngles; Vector3 aimingDir; float angle; aimingDir = lookAtTarget - transform.position; angle = -Mathf.Atan2(aimingDir.z, aimingDir.x) * Mathf.Rad2Deg + 90.0f; angle = Mathf.Round(angle / 90f) * 90.0f; Quaternion qTo = Quaternion.AngleAxis(angle, Vector3.up); wall.transform.rotation = qTo; print(“built wall”);but for some reason, it doesn’t work the way I want it to. In fact, it doesn’t work at all. It only points in one direction, and never turns in the direction of the player.

I’m using Photon Pun for the multiplayer engine. Each building objects has a Photon Transform View attached to it, and a mesh collider so the player’s can collide with it. I put a transform in front of the player so the building object set’s it transform position to that (if a linecast can successfully be completed from the player to the building object point). If an object is blocking the way, the building object will not be instantiated.

Here is the code for my grid (I found it off of this youtube video:website

`````` using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Grid : MonoBehaviour
{
public float size = 1f;
public Vector3 GetNearestPointOnGrid(Vector3 position)
{
position -= transform.position;
int xCount = Mathf.RoundToInt(position.x / size);
int yCount = Mathf.RoundToInt(position.y / size);
int zCount = Mathf.RoundToInt(position.z / size);

Vector3 result = new Vector3((float)xCount * size, (float)yCount * size, (float)zCount * size);
result += transform.position;
return result;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
for(float x = 0; x < 20; x+= size)
{
for(float z = 0; z < 20; z+= size)
{
for(float y = 0; y < 20; y+= size)
{
var point = GetNearestPointOnGrid(new Vector3(x, y, z));
Gizmos.DrawSphere(point, .1f);
}

}a
}
}
}
``````

I made up this script, and it worked!

``````public int increment;
private void Update()
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, (Mathf.Round(transform.eulerAngles.y / increment) * increment), transform.eulerAngles.z);
}
``````