Unity3d vector and matrix operations

Posted by brandon on Game Development See other posts from Game Development or by brandon
Published on 2012-04-13T16:28:41Z Indexed on 2012/04/13 17:43 UTC
Read the original article Hit count: 368

Filed under:
|

I have the following three vectors:

  • posA: (1,2,3)
  • normal: (0,1,0)
  • offset: (2,3,1)

I want to get the vector representing the position which is offset in the direction of the normal from posA.

I know how to do this by cheating (not using matrix operations):

Vector3 result = new Vector3(posA.x + normal.x*offset.x
                             posA.y + normal.y*offset.y,
                             posA.z + normal.z*offset.z);

I know how to do this mathematically Note: [] indicates a column vector, {} indicates a row vector

result = [1,2,3] + {2,3,1}*{[0,0,0],[0,1,0],[0,0,0]}

What I don't know is which is better to use and if it's the latter how do I do this in unity? I only know of 4x4 matrices in unity. I don't like the first option because you are instantiating a new vector instead of just modifying the original. Suggestions?

Note: by asking which is better, I am asking for a quantifiable reason, not just a preference.

© Game Development or respective owner

Related posts about unity

Related posts about matrix