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;
}