- Home /
Create objects in a selected area
I have written code for a loop to create objects in a select area based off mouse position A and Mouse position B. but the problem i have come across is that it only works is if point1 is to the right of and lower to point2, How can write this correctly?
var point1 : Vector3;
var point2 : Vector3;
var z : float;
var ray : Ray; //Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && box2)
{
point1= ray.origin + (ray.direction*distanc);
point1.y = hieghtFloor;
point1.z = Mathf.RoundToInt(point1.z);
z = point1.z;
point1.x = Mathf.RoundToInt(point1.x);
pointStr1 = point1.x+","+ point1.y+"1,"+point1.z;
Debug.Log("Point "+pointStr1);
}
if (Input.GetMouseButtonUp(0)&& box2)
{
point2= ray.origin + (ray.direction*distanc);
point2.y = hieghtFloor;
point2.z = Mathf.RoundToInt(point2.z);
point2.x = Mathf.RoundToInt(point2.x);
pointStr2 = point2.x+","+ point2.y+","+point2.z;
var currentz : int = point2.z- point1.z;
var currentx : int = point2.x-point1.x;
var maxInt : int = currentz*currentx;
if(maxInt > 0)
{
for(var i = 0;i <= currentx; i++)
{
for(var t = 0 ;t <= currentz; t++)
{
if(!GameObject.Find(pointStr1) && !GameObject.Find(pointStr2))
{
currentObj=GameObject.Instantiate (startCube,point1, Quaternion.identity);
pointStr1 = point1.x+","+ point1.y+"1,"+point1.z;
currentObj.name = point1.x+","+ point1.y+"1,"+point1.z;;
}
point1.z++;
pointStr1 = point1.x+","+ point1.y+"1,"+point1.z;
}
point1.x++;
point1.z = z;
pointStr1 = point1.x+","+ point1.y+"1,"+point1.z;
}
}
Answer by Memige · Oct 25, 2012 at 01:19 AM
After you get your second point, do a quick compare to see if the second one is to the top left of the first. if it is, you're good to go, if not, swap the values ie
var temp : Vector3 = point1;
point1 = point2;
point2 = temp;
This should allow your existing code to work identically regardless what the orientation of the two mouse inputs are.
Answer by JaredColl98 · Oct 25, 2012 at 05:31 AM
No i cant get this to work
Ok, my answer was mostly algorithmic, so I'll have a look and see if I can get a practical fix working. To help me get started can you give me more details on how you implemented my suggestion, and what broke?
Your answer
Follow this Question
Related Questions
Mouse Over, Mouse Down, and Instantiate. 1 Answer
Problems with Custom Tool Tip Generation 1 Answer
Trying to not make an infinite loop 0 Answers
Instantiate color change loop 0 Answers