i am struggling with vector math.
I have two objects that have been rotated in the world yet I want to scale in the same direction relative to a specific direction and not to their own forward direction.
I have two cubes
[box1]
rotation = 0, 0, 0
scale = 1, 1, 1
directionToWorld = 1, 0, 0
[box2]
rotation = 0, 90, 0
scale = 1, 1, 1
directionToWorld = 0, 0, 1
No matter their rotation, I want to scale them in the same direction relative to the world.
[world]
direction = 1, 0, 0
lets say i want to multiply their scale based on a new vector of 2,1,1 using the worlds direction.
I want to retrieve the following
[box1]
scale = 2, 1, 1
[box2]
scale = 1, 1, 2
Can anyone give me an idea how to do this. Thank you.
You might have oversimplified your example to the level I am not sure what you want to achieve. If you really want only to rotate your objects by 90 degrees, you can devise a system that swaps the scaling eg. around y by 90 degrees swaps the x and z scaling and so on. With some additional thinking and linear algebra, it is maybe even possible to devise a one-liner function that swaps this automagically.
However if you want to rotate your objects by any other angle (like 45 degrees) , this is not possible. You can only scale your transform in three perpendicular axes. Even if you try to somehow project the world coordinate scaling vector to the gameobject transform coordinate system, the outcome would be different. If you scale square in x axis and then in y axis you get rectangle, if you scale it diagonally, you get rhombus.
That being said, the best solution for not-rightangle-rotations-case in my opinion is this:
Make your gameobjects child of empty gameobject (transform) (every gameobject can have individual parent, or they can be children of the the same one - depends what you want). Rotate only your child gameobjects and keep their parent transform at (0,0,0) rotation. Then scale their parent.