!(http://
photo upload website
) I was curious if anyone knows how to obtain the total length of a raycast that has been reflected multiple times from many object normals? I know how to get either the first initial length(magnitude), or the last one, but not more than 1 reflection.
Say, with the attached photo, I wanted to get length (default meters is fine as that is what my settings are using) of the total of all 4 yellow drawlines here. I have a few codes that are creating this that I will share, as they may help or be useful for additive coding instead of creating a whole new script etc.
Image link:
photo upload website
//basically just sets the starting point of all the rays
Vector3 startingPosition = position;
directDist = Vector3.Distance(transform.position, micRigidbody.transform.position);
//creates the ray or array of rays from a starting point to the NEXT point only. First set of rays only but basis of loop using "ray"
Ray ray = new Ray(position, direction);
RaycastHit hit;
//casts out a ray and creates the hit.point so a line can be drawn
if (Physics.Raycast(ray, out hit, maxStepDistance))
{
direction = Vector3.Reflect(direction, hit.normal);
position = hit.point;
if (hit.transform != micRigidbody.transform)
{
DrawReflections(position, direction, reflectionsRemaining - 1);
distRay = (hit.point - transform.position).magnitude;
}
GameObject Text = GameObject.Find("Text");
Text.GetComponent<Text>().text = distRay.ToString();
}
else
{
position += direction * maxStepDistance;
}
The code attached is the raycasting code and the reflections are created in relation to this code using:
DrawReflections(this.transform.position + this.transform.forward * 0.75f, this.transform.forward, maxReflectionCount);
Hope that helps. I am not curious of how to “better” this code btw either, I will do that along the way, I am simply wondering how to “add” the lengths or magnitudes of each of those yellow lines into one float or int/var etc. In Mathf, I do not see anything to my knowledge that would do this (i.e. Sum), and I have tried a list but am unsure how to stop it from updating a loop though it did get me the closest to getting a bunch of numbers I could add by calculator on windows. It, however, was a List.Add function that I could not find the right way to get it to stop adding to the list, or it was a pain to add and remove and calculate etc all realtime as the rays were moving.
Three days in on this one, reading and testing. So, I have put a lot of time in, for those that want to mention Google search or Unity documents. No attitude needed please. I know how forums can get but I except more from Unity devs as always. Thank you all.