Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Building a custom set of Planes for use with GeometryUtility.TestPlanesAABB.

Discussion in 'Scripting' started by ubhkid, Nov 10, 2011.

  1. ubhkid

    ubhkid

    Joined:
    Jul 31, 2010
    Posts:
    73
    tldr; I need to do the same thing as GeometryUtility.CalculateFrustumPlanes, but with my own bounds.

    Hey everyone. I'm trying to do a standard unit selection method seen in most RTS games. I am drawing a Texture based on a rect I'm creating based on mouse clicks and mouse positions. When I let go of my mouse01 button I take a snap shot of the rect and attempt to build 6 planes for use with GeometryUtility.TestPlanesAABB. Here's where I hit a wall, GeometryUtility.CalculateFrustumPlanes does exactly what I want, but I can't define the points to use, just the camera.

    Image to illustrate what I'm doing:

    **Edit, can't view the image? Click it. Not sure what's wrong.

    **Edit 2, I've decided to use Rect.Contains to over come my problem. If anyone wants to try to tackle my issue still i'm sure others could benefit! :)
     
    Last edited: Nov 10, 2011
  2. ubhkid

    ubhkid

    Joined:
    Jul 31, 2010
    Posts:
    73
    I'll post my solution here for anyone that's interested in the future

    PlayerControl.js- (I have this placed on my camera)
    Code (csharp):
    1.  
    2. function Awake() {
    3.     myTransform = transform;
    4.     thisCamera = myTransform.GetComponent(Camera);
    5.     global = myTransform.GetComponent(Global);
    6.     screenDimensions = Vector2(Screen.width, Screen.height);
    7. }
    8.  
    9. function Start() {
    10.     DetectInput();
    11. }
    12.  
    13. function DetectInput() {
    14.     while(true) {
    15.         mouse1Up = Input.GetButtonUp("Fire1");
    16.         mouse1Down = Input.GetButton("Fire1");
    17.         if(mouse1Down  !dragging) {
    18.             StartDragSelection();
    19.         } else if(dragging  !mouse1Down) {
    20.             StopDragSelection();
    21.         }
    22.         if(dragging) {
    23.             var width : int = Mathf.Abs(mousePos.x - startPosition.x);
    24.             var height : int = Mathf.Abs((screenDimensions.y - mousePos.y) - (screenDimensions.y - startPosition.y));
    25.             boxRect = Rect(Mathf.Min(startPosition.x, mousePos.x), Mathf.Min(screenDimensions.y - startPosition.y, screenDimensions.y - mousePos.y), width, height);
    26.         }
    27.         yield;
    28.     }
    29. }
    30.  
    31. function StartDragSelection() {
    32.     startPosition = mousePos;
    33.     var width : int = Mathf.Abs(mousePos.x - startPosition.x);
    34.     var height : int = Mathf.Abs((screenDimensions.y - mousePos.y) - (screenDimensions.y - startPosition.y));
    35.     boxRect = Rect(Mathf.Min(startPosition.x, mousePos.x), Mathf.Min(screenDimensions.y - startPosition.y, screenDimensions.y - mousePos.y), width, height);
    36.     dragging = true;
    37. }
    38.  
    39. function StopDragSelection() {
    40.     CreateDragSelectionCollider();
    41.     yield;
    42.     dragging = false;
    43. }
    44.  
    45. function CreateDragSelectionCollider() {
    46.     global.ClearSelection();
    47.     var allUnits : GameObject[] = global.allUnitsArr.ToBuiltin(GameObject);
    48.     for(var i = 0; i < global.allUnitsArr.length; i++) {
    49.         var thisUnitSP : Vector3 = thisCamera.WorldToScreenPoint(allUnits[i].transform.position);
    50.         thisUnitSP.y = screenDimensions.y - thisUnitSP.y;
    51.         if(boxRect.Contains(thisUnitSP)) {
    52.             allUnits[i].SendMessage("ClickedOn", true);
    53.         }
    54.     }
    55. }
    56.  
    57. function OnGUI() {
    58.     if(dragging) {
    59.         GUI.skin = skin;
    60.         GUI.Box(boxRect, "", skin.customStyles[0]);
    61.     }
    62. }
    GameManager.js- ("Global" script)
    Code (csharp):
    1. var allUnitsArr = new Array();
    2. var selectionArr = new Array();
    3.  
    4. function AddToAllUnitsArray(newObj : GameObject) {
    5.     allUnitsArr.Push(newObj);
    6. }
    7.  
    8. function ModifySelectionArray(add : boolean, newObj : GameObject) {
    9.     if(!add) ClearSelection();
    10.     selectionArr.Push(newObj);
    11. }
    12.  
    13. function ClearSelection() {
    14.     for(var i = 0; i < selectionArr.length; i++) {
    15.         selectionArr[i].SendMessage("Deselect");
    16.     }
    17.     selectionArr.Clear();
    18. }
     
    LudiKha likes this.