Matrix

Description
This snippet is my matrix class. I use this class when I can't get Unity's internal rotation and 3D math functions to do exactly what I want. It's pretty easy to use and the functions are discrete.

Usage
Below are some basic usage patterns to get you going and to let you see how the class works.


 * Rotate Around Yaw - Rotate an object around it's Y axis.

If you wanted to, this could be done in less code. For example:

Or even:

It all depends on what your code readability tolerance is.


 * Rotate Around Pitch And Yaw - Rotate an object around both it's X and Y axes.

You can easily chain matrix commands together, so the above could be done like this:

That doesn't sacrifice a lot of readability and makes for cleaner looking code.


 * Scale And Translate - Scale an object around it's local coordinate space and move to a location.

Optimizations
If this class becomes a bottleneck for you, you're using it too much. But seriously, one easy optimization to the above code examples is to skip the initial "new Matrix" line. That creates a brand new identity matrix and returns it to you. If you're immediately going to start calling functions that change the matrix you might as well just start with those and save that step. For example, you could change this:

Into this:

And save yourself the object creation line.

Summary
So it should be pretty clear how it works. The basic pattern to follow is: Create a matrix object, stack operations into it (translations, rotation, scaling), and then use that matrix to transform Vector3 objects.

History

 * Article submitted (4/7/2007)