Rotating an object in a 3D grid

I have a system working for building a custom object out of blocks in a 3D grid environment. I manually set the size of each piece in a Vector3 and then add it like so:

	public void AddToGrid (Vector3 gridPos, GunPart _part) {
		//adds a reference to an object for every space it occupies on the grid
		int x = (int)gridPos.x;
		int y = (int)gridPos.y;
		int z = (int)gridPos.z;
			
		for(int sx = 0; sx < _part.size.x; sx++){
			for(int sy = 0; sy > -_part.size.y; sy--){
				for(int sz = 0; sz < _part.size.z; sz++){
					//adding the part at XYZ
					gunGrid[sx + x, sy + y, sz + z] = _part.gameObject;
				}
			}
		}
	}

If I want to be able to rotate the pieces, I’m not sure how to take the euler angles and apply it to that size to accurately shift the X,Y and Z of the size. The origin of each piece is in the top/left/back so I’d also need to know where it’s orienting and how to move along the grid accordingly. I’ve tried to google this but found nothing.

Hello,

This is the main reason why Quaternions are amazing!

If you create a quaternion rotation:

var Q:Quaternion = Quaternion.Euler(0, 90, 0);

You can rotate a Vector3 around the origin (Vector3.zero):

var V:Vector3 = Q*vector;

It really is that simple.

var V:Vector3 = Quaternion.Euler(0, 180, 0)*Vector3.forward;
//V will equal Vector3.backward

It’'s leave the “apply to situation” to you :slight_smile:

Hope this helps,
Benproductions1

Usually you are going to want to use float’s for positions, but it may not be as useful if there is just a grid.
Have you looked into using _part.gameObject.transform.Rotate() ?