0
\$\begingroup\$

I'm trying to convert a list of Vector3 on Vector2 because I need to put them in a graph like this: enter image description here

The graph is easy to do having the Vector2 list, but I have a non-trivial problem obtaining the Vector3 Points:

enter image description here

As you can see, I get them using a Plane which can be in any orientation and rotation.
What formula can I use to convert them so I get X,Y points no matter the rotation of the plane.

enter image description here



PD: I use this code to do the job but it doesn't work on different positions and rotations of the plane...

projectedPosition.X = Vector3.Dot(ballPosition - planeOrigin, planeX);
projectedPosition.Y = Vector3.Dot(ballPosition - planeOrigin, planeY);

PlaneY = Vector3.up & PlaneX = plane.transform.forward

\$\endgroup\$
1
\$\begingroup\$

PlaneY = Vector3.up & PlaneX = plane.transform.forward

One of these things is not like the other. You're using a global axis vector for one, and the plane's axis vector for the other. Try this instead:

PlaneY = -plane.transform.right;
PlaneX =  plane.transform.forward;

Or:

var localPosition = plane.transform.InverseTransformPoint(ballPosition);

projectedPosition.X =  localPosition.Z;
projectedPosition.Y = -localPosition.X;
\$\endgroup\$
3
  • \$\begingroup\$ I see it works correctly, but... I got a problem with the orientation, since the plane can be any Euler angle like (0,90,0) to let's say... (90,0,270) the result sometimes gives me negative results or sometimes the axis X and Y are swapped. How could I solve this in a clean way :( \$\endgroup\$
    – Nakel
    Mar 23 at 1:59
  • \$\begingroup\$ It's unclear to me what outcome you want. This code will give you xy positions corresponding to the positions of the balls in the plane's local space, at any angles you choose. What would you like to be different about the output than what you have currently? \$\endgroup\$
    – DMGregory
    Mar 23 at 2:07
  • \$\begingroup\$ I made a quick video: drive.google.com/file/d/1pCjw-3gDlppw3lwXvyw9rBzUaGrHCu2P/… I want to make clear it works, but sometimes I get negative results on the graph (ok I can get Mathf.Abs for each value on the transform.right and forward to fix that) But also sometimes the X axis get's swapped with Y axis so in the graph you can see. So the final question would be... how to obtain a constant Y values for height and distance on X... THANK YOU VERY MUCH! \$\endgroup\$
    – Nakel
    Mar 23 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.