x


Contextual Rose Menu not determining correct context.

So.... I'm working on a context sensitive rose command menu. You right click and you get a bunch of options that you can select. The idea behind the menu is for advanced commands in a tactical strategy game. I'm trying to make it context sensitive so that a user can right click an enemy unit and get specific advanced commands for that unit.. or click an objective and get specific advanced commands for that as well.

The problem I'm currently having is that the code to determine what is hit isn't coming from the center of the rose menu, where the user hit the right mouse button. Its coming from where the user is left clicking to click the command button.

Not sure if I explained that very well... but regardless of where I right click I'm getting the terrain...and only when I left click do I actually pick up objects.

private var mouseButton1DownPoint : Vector2;
private var mouseButton1UpPoint : Vector2;
private var mouseButton1DownTerrainHitPoint : Vector3;
private var mouseButton2DownTerrainHitPoint : Vector3;
private var mouseButton2DownPoint : Vector2;
private var mouseButton2UpPoint : Vector2;
private var mouseButton3DownPoint: Vector2;
private var mouseButton3UpPoint: Vector2;

private var selectionPointStart : Vector3;
private var selectionPointEnd : Vector3;
private var mouseLeftDrag : boolean = false;
private var mouseRightDrag : boolean = false;
private var mouseRightDown : boolean = false;
private var terrainLayerMask = 1 << 8;
private var nonTerrainLayerMask = ~terrainLayerMask;
private var raycastLength : float = 200.0;
// semi transparent texture for the selection rectangle
var selectionTexture : Texture;
// 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;

private var debug = true;

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

 // Right mouse button
 if (mouseRightDown) {
            var hit : RaycastHit;
            var ray = Camera.main.ScreenPointToRay (mouseButton2DownPoint);

                if(mouseCoordsSet) {
                    //Do nothing
                    print("Mouse Coords already defined. " + mouseButton2DownPoint);
                } else {
                    //Define rose menu button coords
                    //Mouse Coords
                    mouseButton2DownPoint = Event.current.mousePosition;
                    roseOriginPoint = mouseButton2DownPoint;
                    print("Setting Rose Origin to: " + mouseButton2DownPoint + "\n Origin is: " + roseOriginPoint);

                    //Home Graphic
                    mouse0Coord1 = mouseButton2DownPoint[0] -10;
                    mouse0Coord2 = mouseButton2DownPoint[1] -10;

                    //Move Button Coords
                    mouseCoord1 = mouseButton2DownPoint[0] -40;
                    mouseCoord2 = mouseButton2DownPoint[1] -55;

                    //Waypoint Button Coords
                    mouse2Coord1 = mouseButton2DownPoint[0] -40;
                    mouse2Coord2 = mouseButton2DownPoint[1] +15;

                    //Attack Button Coords
                    mouse3Coord1 = mouseButton2DownPoint[0] -40;
                    mouse3Coord2 = mouseButton2DownPoint[1] -100;
                    mouseCoordsSet = true;
                }

                //Draw Rotary UI
                GUI.Box (Rect (mouse0Coord1,mouse0Coord2,20,20), " ");

                //Move Button
 if(GUI.Button (Rect (mouseCoord1,mouseCoord2,75,40), "Move.")) {
                    //Run Move function
                    if (UnitManager.GetInstance().GetSelectedUnitsCount() == 0) {
 // no units selected, do nothing
                    } else {
 // untis are selected, move them. Unit Manager's unit count is > 0!
 UnitManager.GetInstance().MoveSelectedUnitsToPoint(mouseButton2DownTerrainHitPoint);
                    }
                }

                //WayPoint
                if(GUI.Button (Rect (mouse2Coord1,mouse2Coord2,75,40), "Set\n Waypoint.")) {
                    print("Way Point Command Pressed");
                }

                //Attack
                if(GUI.Button (Rect (mouse3Coord1,mouse3Coord2,75,40), "Attack!")) {
                    //print("Attacking");
                    determineRoseContext(roseOriginPoint);
                }
 }
}

function determineRoseContext(screenPosition : Vector2) {
    mouseButton2DownPoint = screenPosition;
    var hit: RaycastHit;

    var ray = Camera.main.ScreenPointToRay (mouseButton2DownPoint);
 //Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
 if ( Physics.Raycast (ray, hit, raycastLength) ) { // terrainLayerMask
 //Debug.DrawRay (ray.origin, ray.direction * 100.0, Color.green);
                        print ("Mouse Down Hit something: " + hit.collider.name + " At Coordinates: " + mouseButton2DownPoint);
 }
}
more ▼

asked Jul 19 '12 at 03:03 AM

DarthViper3k gravatar image

DarthViper3k
1 3

I used mouseRightDown as a neat variable. I have another section I omitted as it's not relevant to the error.

// Right mouse button if (Input.GetButtonDown("Fire2")) { mouseRightDown = true; }

roseOriginPoint was defined right above the different button coordinates.

Jul 19 '12 at 05:15 PM DarthViper3k

well, I think the problem is in a part of the code we're not seeing.. Do the print lines come out as expected?? Also, I don't see where mouseButton2DownTerrainHitPoint gets set.. I would start by adding some Debug.Logs. like Debug.Log("screenPosition") in the last method there.. unless I'm misunderstanding the issue.. if you're actually saying that the hit when you right click is always terrain, then it's probably the LayerMask, but I can't see that being used here

Jul 19 '12 at 05:56 PM Seth Bergman
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

based on the code here it all looks ok.. but you're using "if(mouseRightDown)"... what is that? I suspect it may be getting called even on left button clicks.. try:

if(Input.GetMouseButtonDown(1))

instead, I bet that solves it

Also, where is "roseOriginPoint" defined?

more ▼

answered Jul 19 '12 at 09:21 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

(comments are locked)
10|3000 characters needed characters left

I used mouseRightDown as a neat variable.. I have another section I omitted as its not relevant to the error.

// Right mouse button
 if (Input.GetButtonDown("Fire2")) {
 mouseRightDown = true;
 }

Also I added roseOriginPoint because I thought perhaps mouseButton2DownPoint was getting a new set of coordinates before I passed the coordinates over to the determineRoseContext function.

I had it print out the mouse coordinates being used to try and figure out why its only picking up the terrain.... The coordinates used in determineRoseContext() are the same coordinates used to display the rose menu. Which is what makes this so odd.

more ▼

answered Jul 19 '12 at 06:15 PM

DarthViper3k gravatar image

DarthViper3k
1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5076
x1528
x39

asked: Jul 19 '12 at 03:03 AM

Seen: 236 times

Last Updated: Jul 19 '12 at 06:15 PM