instantiate along a line while dragging

Hi

I am trying to create a wall made from cubes during game play. A user clicks mouse on screen to set the initial position, then drags and releases at the end position. My script at the moment creates the first and last prefab as well as a prefab at the midpoint between the two. The first prefab appears as expected. The midpoint and last prefab appear after I release and click the button again. (also three more prefabs are created on second click). What I would like is to see all prefabs as they change position in order to visualize their placement as I hold the button down and drag.

Thanks

using UnityEngine;
using System.Collections;

public class instance : MonoBehaviour {

public GameObject myPrefab;

public Vector3 startPoint;
public Vector3 midPoint; 
public Vector3 endPoint; 

	void Start () {
	
	}	
 
void Update () {

       if ( Input.GetMouseButtonDown(0) ) {
       	
        startPoint = Input.mousePosition;
        startPoint.z = 1.0f;
        Make();

       }
         if ( Input.GetMouseButton(0) ) {
         	     	
         endPoint = Input.mousePosition;
         endPoint.z = 1.0f;
         midPoint = (endPoint-startPoint)/2.0f + startPoint;
         midPoint.z = 1.0f;
         
       }
}
void Make () {
	         
	Vector3 startPos = Camera.main.ScreenToWorldPoint(startPoint);
	Vector3 midPos = Camera.main.ScreenToWorldPoint(midPoint);
	Vector3 endPos = Camera.main.ScreenToWorldPoint(endPoint);
	          
Instantiate(myPrefab, startPos, Quaternion.identity);
Instantiate(myPrefab, midPos, Quaternion.identity);
Instantiate(myPrefab, endPos, Quaternion.identity);
}
}

For anyone who is interested, this is how I did it. Works as expected now. Creating three blocks at start, midpoint and end. Now the NEXT STEP is this… How do I fill the created line with cubes, or any other prefab so they don’t overlap? I know the distance between start point and end point and can round it up or down. I know the dimensions of the cube or other prefab. Instead of instantiating one object at midpoint I can divide the distance by prefab width. That would give me the number of prefabs to create, but then how do I space them along the line?

Thanks for any help.

using UnityEngine;
using System.Collections;

public class instance : MonoBehaviour {

public GameObject myPrefab;
public Vector3 startPoint;
public Vector3 midPoint; 
public Vector3 endPoint;
public float mouseDistance;
public bool trackMouse = false;
private GameObject endBlock;
private GameObject midBlock;
private GameObject startBlock;
 
void Update () {
	   
	   Vector3 startPos = Camera.main.ScreenToWorldPoint(startPoint);
	   Vector3 endPos = Camera.main.ScreenToWorldPoint(endPoint);
	   Vector3 midPos = Camera.main.ScreenToWorldPoint(midPoint);

       if (Input.GetMouseButtonDown(0)) {
		startBlock = (GameObject) Instantiate(myPrefab, startPos, Quaternion.identity);
		midBlock = (GameObject) Instantiate(myPrefab, midPos, Quaternion.identity);
		endBlock = (GameObject) Instantiate(myPrefab, endPos, Quaternion.identity);
        startPoint = Input.mousePosition;
        startPoint.z = 1.0f;
        trackMouse = true;
       }
       
        if (Input.GetMouseButtonUp(0)) {
        trackMouse = false;
        mouseDistance = 0;
   		}
     	
     	if (trackMouse){
     	startBlock.transform.position = startPos;
     	midBlock.transform.position = midPos; 
     	endBlock.transform.position = endPos;
        endPoint = Input.mousePosition;
        endPoint.z = 1.0f;
        midPoint = (endPoint-startPoint)/2.0f + startPoint;
        midPoint.z = 1.0f;
        mouseDistance = (endPoint - startPoint).magnitude;
     	}     
	}
}

Amazing
Copied from my post from just 14 hours ago

I suppose I need to make an array to which game objects need to be added as distance increases and remove them from the array if distance shrinks. Then update the position of each object in the array while the mouse is being dragged. I haven’t done much array stuff that can dynamically change size like that, plus having the objects within it updating too. Any help with logic would be appreciated. Thank you.

I’ve done it! Well… to a point. I used a Bresenham Line drawing algorithm I got from wikipedia.
Now I can build a line from prefabs. Still have a few questions. At the moment I am creating and destroying the prefabs, which I am told is a slow process. How do I speed it up?
Secondly, I would like the line to be persistent, so I can draw one line and it remains on screen when I draw a new one. Because I’m destroying objects with tag “lines”, which is what the prefab is tagged as, should I change the tag once I create the line?
Thirdly, I would like the lines to maintain editability, so if I click on the end block, I can change the line again. Or delete it all together.

Anyway, here is the code. Someone may find it useful.

public var myPrefab : GameObject;
private var startPoint : Vector3;
private var endPoint : Vector3;
public var startPos : Vector3;
public var endPos : Vector3;

function Update() {
	startPos = Camera.main.ScreenToWorldPoint(startPoint);
	endPos = Camera.main.ScreenToWorldPoint(endPoint);

	if (Input.GetMouseButtonDown(0)) {
				startPoint = Input.mousePosition;
        		startPoint.z = -12.0f;
	}
	if (Input.GetMouseButton(0)) {
				endPoint = Input.mousePosition;
        		endPoint.z = -12.0f;
				myPoints = GameObject.FindGameObjectsWithTag("lines");
				for (points in myPoints) {
  				Destroy(points);
				}
	drawLine(startPos.x,startPos.y,endPos.x,endPos.y);
	}				
}
		
 function drawLine(x0:int, y0:int, x1:int, y1:int)
 {
   	dx= Mathf.Abs(x1-x0);
   	dy= Mathf.Abs(y1-y0);
   	if (x0 < x1) {sx=1;}else{sx=-1;}
   	if (y0 < y1) {sy=1;}else{sy=-1;}
   	err=dx-dy; 
	loop = true;
	
	while (loop) {
		 Instantiate(myPrefab, Vector3(x0, y0, -12), Quaternion.identity);
		 if ((x0 == x1)  (y0 == y1)) loop=false;
		 e2 = 2*err;
		 if (e2 > -dy) {
		   err = err - dy;
		   x0 = x0 + sx;
		 }
		 if (e2 <  dx) {
		   err = err + dx;
		   y0 = y0 + sy;
		}
	}
}

Hello Sir,

Just used you code .

It’s giving many errors like-

Assets/Scripts/NewBehaviourScript.js(41,12): BCE0005: Unknown identifier: ‘err’.
Assets/Scripts/NewBehaviourScript.js(42,22): BCE0005: Unknown identifier: ‘sx’.
etc…

Any help?