- Home /
How to fill selected area with prefabs?
Hello, I am making a 3D top-down game.
I have a JS script that allows me to drag a texture like a selection box in an RTS game. At the end of the selection I want to fill the selected area automatically with prefabs.
I just dont know how..
private var mouseButton1DownPoint : Vector2;
private var mouseButton1UpPoint : Vector2;
private var mouseButton2DownPoint : Vector2;
private var mouseButton2UpPoint : Vector2;
private var mouseLeftDrag : boolean = false;
private var raycastLength : float = 200.0;
// semi transparent texture for the selection rectangle
var selectionTexture : Texture;
// floor Spawner
var ConstGrid : GameObject;
// range in which a mouse down and mouse up event will be treated as "the same location" on the map.
private var mouseButtonReleaseBlurRange : int = 20;
function OnGUI() {
if (mouseLeftDrag) {
var width : int = mouseButton1UpPoint.x - mouseButton1DownPoint.x;
var height : int = (Screen.height - mouseButton1UpPoint.y) - (Screen.height - mouseButton1DownPoint.y);
var rect : Rect = Rect(mouseButton1DownPoint.x, Screen.height - mouseButton1DownPoint.y, width, height);
GUI.DrawTexture (rect, selectionTexture, ScaleMode.StretchToFill, true);
}
}
function Update ()
{
// Left mouse button
if (Input.GetButtonDown("Fire1")) {
Mouse1Down(Input.mousePosition);
}
if (Input.GetButtonUp("Fire1")) {
Mouse1Up(Input.mousePosition);
}
if (Input.GetButton("Fire1")) {
// Used to determine if there is some mouse drag operation going on.
Mouse1DownDrag(Input.mousePosition);
}
//Right mouse button
if (Input.GetButtonDown("Fire2")) {
Destroy(gameObject);
}
}
function Mouse1DownDrag(screenPosition : Vector2) {
// Only show the drag selection texture if the mouse has been moved and not if the user made only a single left mouse click
if (screenPosition != mouseButton1DownPoint) {
mouseLeftDrag = true;
// while dragging, update the current mouse pos for the selection rectangle.
mouseButton1UpPoint = screenPosition;
var hit : RaycastHit;
ray = Camera.main.ScreenPointToRay (screenPosition);
if ( Physics.Raycast (ray, hit, raycastLength) )
{
selectionPointEnd = hit.point;
}
}
}
function Mouse1Down(screenPosition : Vector2) {
mouseButton1DownPoint = screenPosition;
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (mouseButton1DownPoint);
if ( Physics.Raycast (ray, hit, raycastLength))
{
mouseButton1DownTerrainHitPoint = hit.point;
selectionPointStart = hit.point;
}
}
function Mouse1Up(screenPosition : Vector2) {
mouseButton1UpPoint = screenPosition;
var hit : RaycastHit;
mouseLeftDrag = false;
}
Answer by erick_weil · Aug 07, 2014 at 07:41 PM
ok, i did'nt uderstanded well your code, but to fill a retangle with prefabs knowing his start point and end point is simple:
start._____________
! !
! !
! !
! !
!____________!. end
if your prefabs are circles with a diameter of 1 unit, you only need to arrange two loops and put then inside the retangle:
function retangule (StartPoint : Vector3,EndPoint : Vector3) {
var Retangle : Vector3;
Retangle = EndPoint - StartPoint;
var width : int = Retangle.x/radius;
var height : int = Retangle.z/radius;
var d : Vector2 = Vector2(1,1);
if( width <0)
{
width = -width;
d.x = -1;
}
if( height <0)
{
height = -height;
d.y = -1;
}
for(var x=0;x<width;x++)
{
for(var z=0;z<height;z++)
{
var px : float = StartPoint.x+(x*radius*d.x);
var pz : float = StartPoint.z+(z*radius*d.y);
Instantiate(prefab,Vector3(px,0,pz),prefab.transform.rotation);
}
}
}
}
thanks for your help!
It's probably just me ... but I can't get your script to run. I have changed the axes because for me Z is responsible for height and I also don't get the radius idea... is it to get the prefab scale ?
the radius is the prefab scale, then must be greather than zero and smaller than the given area, are you using unity2D? i tested this in unity3d setup, you need to remember to call the function giving the start and endpoint that make enought space to fit at least one prefab with the given "radius".
Your answer
Follow this Question
Related Questions
Raycasting a specific square area 1 Answer
Bottom area of a 3D model? Raycast? 1 Answer
Select instantiated object with mouse 1 Answer
C# mouse Raycast question 3 Answers
Move selected object. 1 Answer