How to Rotate Plane of Cube Around its Center? (Rotate Vectors Around Point/Axis/Direction)

Hello! I am trying to create a script to deform a cube. Specifically to rotate the top and bottom planes of the cube. I know which vertices of the mesh I need, and I can rotate them, just not in the way I would like to.

Right now I start of with the cube, the direction vector in red, and the top plane in green:

Starting Cube

Then I rotate it using this code:

//Where topRotation is how much I want the plane to be rotated
//And Vector3.right is the red arrow (direction vector)
Quaternion qAngle = Quaternion.AngleAxis(topRotation, Vector3.right);

//Doing this for each vertice
vertice[verticeBeingModified] = qAngle * vertice[verticeBeingModified];

//Then I reset the vertices of the mesh, recalculate normals, ect

And I get something that looks like this: (when the topRotation is near 90)

Rotated Cube

So it appears that the plane is rotating around an axis in the middle of the cube, instead of one in the center of the plane. What I would like to happen is something like this:

What I would like to happen

Where the center of the plane always stays in the same location.

I hope the pictures weren’t too confusing! I have looked into rotating Vector3’s around points & arbitrary direction axis, but I just can’t figure out what exactly I need in this situation. If anyone has any advise/ can point me in the right direction it would me very much appreciated. TY!

Your vertices are specified in local space coordinates of your cube object. Rotations always happens around the origin of the space where you define your positions / vectors. You would have to do:

  • offset your vertices so the new origin is at the center of your plane and not at the center of your cube.
  • rotate your vertices.
  • offset them back by the same amount as in step 1, just in the opposite direction.

Those 3 operations can be combined into a single transformation matrix. But if you’re not familiar with matrices it might be easier for you to do the 3 steps manually.