- Home /
Getting the opposite vector3 from a central point
Hello, I am trying to make a rope swing system.
I need to get a vector3 position "opposite" an original position to get the end point of each swing.
The system consists of the hook point and the player, what I want is for a new vector to be created at a position on the opposite side of the hook as if the player location is being mirrored.
Here is an diagram of what, I mean though the "Reversed position" should be labeled mirrored or opposite.
Any help would be greatly appreciated.
Thanks.
Answer by Sabre-Runner · Nov 16, 2018 at 04:34 AM
If you define Original Position as P1, Central Point as P2 and Reversed Position as P3...
V21 is the vector from P2 to P1, defined as (P1 - P2).
So, how do you get the second vector.
You can do it rather easily if you "Rotate V21 180 degrees around the world Y axis".
This rotation can be achieved like this: Quaternion.AngleAxis(180, Vector3.up).
So, the vector you want, V23, is Quaternion.AngleAxis(180, Vector3.up) * V21.
Now, P3 = P2 + V23.
That's the math.
The code is...
P3 = P2 + (Quaternion.AngleAxis(180, Vector3.up) * (P1 - P2));
Hope this helps.
Thanks, that worked perfectly. I didn't know about Quaternion.AngleAxis, that will be very helpful in the future.
Set up a search shortcut to Unity's API Reference. I always look there for things by association. Quaternions are difficult but it's worth it reading up on what they can do. $$anonymous$$ethod names by themselves aren't always very informative. Good luck and have fun.