@@ -137,31 +137,15 @@ public static Vector3 GetPointByDistance(RayStep[] steps, float distance)
137137 Debug . Assert ( steps != null ) ;
138138 Debug . Assert ( steps . Length > 0 ) ;
139139
140- Vector3 point = Vector3 . zero ;
141- float remainingDistance = distance ;
142- int numSteps = steps . Length ;
143-
144- for ( int i = 0 ; i < numSteps ; i ++ )
140+ var ( rayStep , remainingDistance ) = GetStepByDistance ( steps , distance ) ;
141+ if ( remainingDistance > 0 )
145142 {
146- if ( remainingDistance > numSteps )
147- {
148- remainingDistance -= numSteps ;
149- }
150- else
151- {
152- point = Vector3 . Lerp ( steps [ i ] . Origin , steps [ i ] . Terminus , remainingDistance / steps [ i ] . Length ) ;
153- remainingDistance = 0 ;
154- break ;
155- }
143+ return Vector3 . Lerp ( rayStep . Origin , rayStep . Terminus , remainingDistance / rayStep . Length ) ;
156144 }
157-
158- if ( remainingDistance > 0 )
145+ else
159146 {
160- // If we reach the end and still have distance left, set the point to the terminus of the last step
161- point = steps [ numSteps - 1 ] . Terminus ;
147+ return rayStep . Terminus ;
162148 }
163-
164- return point ;
165149 }
166150
167151 /// <summary>
@@ -170,36 +154,31 @@ public static Vector3 GetPointByDistance(RayStep[] steps, float distance)
170154 /// <param name="steps"></param>
171155 /// <param name="distance"></param>
172156 /// <returns></returns>
173- public static RayStep GetStepByDistance ( RayStep [ ] steps , float distance )
157+ public static ( RayStep rayStep , float traveledDistance ) GetStepByDistance ( RayStep [ ] steps , float distance )
174158 {
175159 Debug . Assert ( steps != null ) ;
176160 Debug . Assert ( steps . Length > 0 ) ;
177161
178- RayStep step = new RayStep ( ) ;
179162 float remainingDistance = distance ;
163+
180164 int numSteps = steps . Length ;
165+ float stepLength = 0 ;
181166
182167 for ( int i = 0 ; i < numSteps ; i ++ )
183168 {
184- if ( remainingDistance > steps [ i ] . Length )
169+ stepLength = steps [ i ] . Length ;
170+
171+ if ( remainingDistance > stepLength )
185172 {
186- remainingDistance -= steps [ i ] . Length ;
173+ remainingDistance -= stepLength ;
187174 }
188175 else
189176 {
190- step = steps [ i ] ;
191- remainingDistance = 0 ;
192- break ;
177+ return ( steps [ i ] , remainingDistance ) ;
193178 }
194179 }
195180
196- if ( remainingDistance > 0 )
197- {
198- // If we reach the end and still have distance left, return the last step
199- step = steps [ steps . Length - 1 ] ;
200- }
201-
202- return step ;
181+ return ( steps [ steps . Length - 1 ] , remainingDistance ) ;
203182 }
204183
205184 /// <summary>
@@ -213,7 +192,7 @@ public static Vector3 GetDirectionByDistance(RayStep[] steps, float distance)
213192 Debug . Assert ( steps != null ) ;
214193 Debug . Assert ( steps . Length > 0 ) ;
215194
216- return GetStepByDistance ( steps , distance ) . Direction ;
195+ return GetStepByDistance ( steps , distance ) . rayStep . Direction ;
217196 }
218197
219198 #endregion
0 commit comments