I am shooting a raycast on a 3d sphere and i want a method which can get a float number between 0 (center of spehere) to 1 (circumference or outline of sphere) depending upon the raycasthit point (position). How can i achieve this.
I have tried calculating diffrence between the position of sphere and raycast impact point, it works somewhat but is very complex to implement in code.
Is there any other way of doing this or am i missing some math functions or vector functions which can help in this case.
In the end it’s just the pythagorean theorem. So you could calculate the vector length yourself if you really want, though it would be just re-inventing the wheel
Note that this just gives you the distance from the sphere center to the hit point. In order to get a value between 0 and 1 you have to know the “highest” / furtherst point on the sphere and divide the distance you get from above by the distance to the highest point. That’s a property od your spherical object so we can not tell you that.
Keep in mind, if you raycast against a sperical sphere, your hit point will always be the same distance from the center of the sphere, that value will be the radius of the sphere.
I guess you’re actually trying to get something else, but you havent described it enough
That’s true if you have a sphere collider. Though, at least from what he has said, I guess he may have either objects on the surface that he hits, or it’s actually an irregular mesh like a planet with hills and valleys. Yes, raycasting against a smooth sphere would be pretty pointless ^^.
In my case its actually a smooth 3d sphere and i am shooting on its surface with raycast. I want to calculate the distance between the centre of this sphere and the point of contact of raycast hit. Then, remap this value to a 0 - 1 scale 0 being centre and 1 the outer line of sphere.
I hope you get what i am trying to achieve.
I thought that might the case, rather than bumpy sphere.
You want to calculate the Dot Product of the ray direction (or camera forward) and the hit.normal
You might then need to remap the result, because it might not be linear between center and edge (cant guess without testing myself), but you’ll still get a useable 0->1