Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DarthViper3k · Jul 19, 2012 at 03:03 AM · raycastmouse position

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);
  }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DarthViper3k · Jul 19, 2012 at 05:15 PM 0
Share

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.

avatar image Seth-Bergman · Jul 19, 2012 at 05:56 PM 0
Share

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 Layer$$anonymous$$ask, but I can't see that being used here

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Seth-Bergman · Jul 19, 2012 at 09:21 AM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DarthViper3k · Jul 19, 2012 at 06:15 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Custom Collision Detection 4 Answers

i am making an increment of one when user clicks a game object, trying to decrement the counter if the same game object is clicked again 1 Answer

physics2d.raycastall help please 1 Answer

Scripting to receive a touch over a specific area 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges