Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Madswint · Apr 18, 2014 at 08:53 PM · guiright click

Right click, gui.box appears at mouse position, freezed?

I have this code. I'm trying to make it like runescape, you right click, and a drop down menu appears, with option for where you rightclicked. It works! All I need it to do, is not follow the mouse cursor after it's been right clicked! Any ideas?

 #pragma strict
 
 var hit : RaycastHit;
 
 var RightClickTrue = false;
 
 var shopOpen : boolean = false;
  
  //
  var boxOpen: boolean = false;
 var boxPos: Vector3;
  //
 function OnGUI () {
  
  
 if(shopOpen == true) {
  GUI.Box(ResizeGUI(Rect(350,350,50,50)), "General Shop");
        
 }
 
 if(RightClickTrue == true) {
 
 }
  
  
     if (boxOpen == true) {
         GUI.Box (Rect (boxPos.x,boxPos.y,120,120), ""); 
  
  }
  
  if (RightClickTrue == true) {
                        boxPos.x = Input.mousePosition.x;
         boxPos.y = Screen.height - Input.mousePosition.y;
         boxOpen = true;
                        }
 }
  
  
  
  
  
  
  
  
 function Update () {
  
  
 if(Input.GetMouseButtonDown(1) &&
        collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,
                         Mathf.Infinity)) {
                        
                        
                        
           
         RightClickTrue = true;            
        }                
 }
 
 
 
 function ResizeGUI(_rect : Rect) : Rect {
 
     var FilScreenWidth = _rect.width / 800;
     var rectWidth = FilScreenWidth * Screen.width;
     var FilScreenHeight = _rect.height / 600;
     var rectHeight = FilScreenHeight * Screen.height;
     var rectX = (_rect.x / 800) * Screen.width;
     var rectY = (_rect.y / 600) * Screen.height;
  
     return Rect(rectX,rectY,rectWidth,rectHeight);
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by koray1396 · Apr 18, 2014 at 08:58 PM

Use another variable;

 if(!RightClickTrue){
 mousePos = Input.mousePosition;
 } else {
 boxPos = mousePos;
 }
Comment
Add comment · Show 3 · 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 Madswint · Apr 18, 2014 at 09:25 PM 0
Share

It gives me a error, it doesn't know what mousePos is.

avatar image koray1396 · Apr 19, 2014 at 05:53 AM 1
Share

of course you have to declare it first, above was intended for a general idea of how it would function.

Actually, looking at your script I realized there is no need for another variable, you already have more than needed.

so it should be

 var boxPos : Vector2; // no need for Vector3, you only use x and y

then under Update(), you should be storing the mouse position with the click.

 if(Input.Get$$anonymous$$ouseButtonDown(1) &&    collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit,$$anonymous$$athf.Infinity)) {
     RightClickTrue = true;
     boxPos.x = Input.mousePosition.x;
     boxPos.y = Screen.height - Input.mousePosition.y;    
 }

then under OnGUI();

 if(RightClickTrue == true) {
     GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "")    
 }

you can also remove boxOpen, since RightClickTrue will be true only when the box opens.

hope this helps.

avatar image Madswint · Apr 19, 2014 at 12:09 PM 0
Share

Ohh thanks a ton! It helped!

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inventory GUI 0 Answers

Detect Right Click on GUI Buttons 2 Answers

4.6 GUI Button inside a scrolling Text Box 0 Answers

c# - void method from external class. 2 Answers

drawing a box with GUI 3 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