Need to find a point in a line using a percentage and a Vector3 list.

Hello!

I need some advice or direction on how to take on a problem I’m facing.

I have a Vector3 list. These Vectors resemble a line (Visualised by a Line Renderer).
Image example:
[38490-screen+shot+2015-01-09+at+14.32.41.png|38490]

This line is created using the Vector3 list which is created by “drawing” with the mouse.
I have the length of the line. Since the length varies, it won’t be a fixed value.
What I want to accomplish, is to use a percentage (of the length of the line), to find a Vector3 point in this line.

Example:

In the picture above, say I would like to get the point at 20%
This is the point where I’d like to get the Vector3 of:
[38491-screen+shot+2015-01-09+at+142.png|38491]

(Not sure if this the exact point, but I hope you get the idea)

I have no idea how to do this, so could you please help me?

I have googled around using percentages ect. but those were for Vector’s between 2 points; this is a whole list of Vectors. I couldn’t find anything about that.

Friendly regards.

This is untested but should work, note that the length of the curve supplied is obviously not just the last point minus the first point!

Vector3 FindPoint(Vector3[] vectors /* your vector array */, float length /* length of curve */, float p /* percentage along the line 0 to 1 */){
	if(vectors.Length < 1){
		return Vector3.zero; // if the list is empty
	}else if(vectors.Length < 2){
		return vectors[0]; //if there is only one point in the list
	}
	
	float dist = length*Mathf.Clamp(p, 0, 1);
	Vector3 pos = vectors[0];
	
	for(int i = 0; i < vectors.Length-1; i++){
		Vector3 v0 = vectors*;*
  •  Vector3 v1 = vectors[i+1];*
    
  •  float this_dist = (v1-v0).magnitude;*
    
  •  if(this_dist < dist){*
    
  •  	dist -= this_dist; //if the remaining distance is more than the distance between these vectors then minus the current distance from the remaining and go to the next vector*
    
  •  	continue;*
    
  •  }*
    
  •  return Vector3.Lerp(v0, v1, dist/this_dist); //if the distance between these vectors is more or equal to the remaining distance then find how far along the gap it is and return*
    
  • }*

  • return vectors[vectors.Length-1];*
    }
    Hope that helps, let me know if it produces any errors :slight_smile:
    Scribe