So, for example, I have a flat sprite that is a circle in my 3D scene, and I want to measure the length of the circle along the x-axis. However, as it’s a circle, there are many different values that this could be.
As shown in the image above, depending on where the length is measured in my case, the value can change. So basically, I’m wondering how I can measure the length at a specific point. The code to measure this would return a single floating point number for the length of the circle.
As additional information, in my scene the sprite will move along the z axis from positive z to negative z, so I wish to measure how the value of the length (measured along the x axis) changes at z = 0, sort of like a cross section. However, I am not sure of how to write the code to find and return the length.
Ok, so I found a solution that works. I was inspired by @Llama_w_2Ls 's answer.
It’s been a while since I solved this, but I decided to post my answer if others require it. During my time working on the project, I actually converted it from being on the x and z axis to being on the x and y axis, and swapped out all 3D hitboxes for 2D hitboxes as I required the Polygon2D Collider. However, the solution I provide below should work with 3D objects as well, so long as you use the 3D equivalent of everything.
At first, I tried what Llama_w_2Ls suggested, which was passing an object through the trigger, and recording its point of entry with OnTriggerEnter() and OnTriggerExit(). However, the issue is that my ‘circle’ moves too quickly, and making the object pass through the trigger either results in a) Width being outputted too slowly, when the object being passed through is too slow, or b) The width being inaccurate, when the object being passed through is too fast.
Instead, what I did was use Raycast2D. I cast one ray from the left and another from the right and use RaycastHit2D.point (and add a .x to get the x coordinate). Then just subtract the right point from the left, and you get the width. With this method, the width is outputted each frame and is also accurate.
Well simply, you would need to use the mathematical equation for constructing this shape, which is x^2 + y^2 = r^2, where r is the radius of your circle, and y is the height at which you’re trying to find the values of x. Find the two values of x (since it is a quadratic equation), and then subtract the smaller one from the larger one to find the length between the two points. @JLaiCCS