Instantiate multiple prefabs evenly along linecast


Much like the title of the question, I’m trying to instantiate prefabs evenly along a linecast. I have it almost working but can’t quite get it to behave like I want and hoped there may be a better way to go about it. At the moment I linecast from a position to mouse cursors pos in world. I check the distance then use a for loop to run through the distance and instantiate a new point based on the direction gathered from the start and end point and each instantiated object is the full distance minus the iteration of the for loop multiplied by .5 to get a half unit.

`if(Physics.Linecast(this.transform.position, mousePosInWorld, out rayHit, layerMask))
lineDistance = Vector3.Distance(this.transform.position, rayHit.point);
float stopDistance = .5f;
Vector3 distanceVec = rayHit.point - this.transform.position;

		for(int x = 0; x < lineDistance; x++)
			positionForNextPoint = this.transform.position;

			positionForNextPoint += distanceVec.normalized * (distanceVec.magnitude - (x * stopDistance));
			GameObject newPoint = Instantiate(PointPrefab, positionForNextGrapplePoint, Quaternion.identity) as GameObject;


The aim is that if the distance is say 8 that it’ll evenly distribute 16 prefabs every half unit of distance. But I have only been able to get it to sort of work when trying things like increasing the distance by 1.5 times which makes points instantiate but there not quite even and the further the distance the larger the gap between the main object and the last instatiate prefab.

Hopefully the question makes sense, does anyone have a better way of going about this?

Just after posting this I realised where I was going wrong. Its pretty stupid of me to be honest . I’m gathering half units with 0.5f so I just needed to use 2 times the distance to compensate. I changed the for loop to x < distance*2 and now everything is fine.