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