Rotate vector to local space

I’m trying to figure out how to rotate a vector so that the ‘z’ is aligned to the ‘z’ of the local space.

I’m pretty bad at vector math, so I have no idea where to go.


It’s a little hard to properly convey exactly what I want.

I want to modify two axes of the vector, but leave the last one intact, but this has to work at any angle.

You can use Vector.x, Vector.y, Vector.z to handle specific vector section-y thingies, just an FYI.
So Vector(50,50,500).z should return 500. I don’t use this often so I’m not totally sure how it works.

But if you multiply a Vector by Vector(1,1,0), it should preserve the X and Y while setting the Z to 0

Or am I completely missing what you’re asking?

Are you talking about this?

I’m pretty sure you need to use RotateAroundAxis for this, however I’ve never managed to understand that function works correctly, I just play with the numbers until it magically just works.

Old wiki example

I’ll try to explain it further.

I have a grid and I want to snap the x and y axes of the grid, but I want to leave the z, or the normal, the same. Because the grid can be at any angle, I don’t know how to do this.

if your vectors are position vectors above/below a grid and you want to snap x and y to the grid, just round off x and y components of the vector and leave the z alone

although I have a sneaking suspicion that may not be what you’re looking for because if by “rotating” you mean actually rotating the vector and keeping its magnitude intact, i have no idea what you’re trying to do

I can’t just change the x and y directly because the grid could be 1, 0, 0 or 0, 0, 1 or even 0.4, 0.1, -0.22 or some shit. This is why I believe I need to do something to change it locally.

Basically, I need the grid to always touch the vector it’s at.

I want to snap the grid itself.