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 Stealthygolem · Jan 07, 2015 at 03:42 PM · uiraycasttransformworldtoscreenpoint

Correct use of transform.position of UI element into game screen

Hello! I am trying to get my UI to transform into the position of my mouse cursor, when I click the mouse button. To give you an idea visually: I am making an action box for an adventure game in 2D Unity. So, whenever you rightclick (Mouse1) there will pop up an action menu at the right-clicked position in the game.

I have tried playing around with WorldToScreenPoint, but I get confused on how I really get this to work. Here is some of my code:

 public class FrankMove : MonoBehaviour {
 
     bool isMoving;
 
     public bool showActionMenu;
     GameObject actionMenu;
     GameObject actionMenuBox;
 
     private float Xdif;
     private float Ydif;
 
     public float speed;
 
     private Vector2 positionHit;
     private Vector2 positionDirection;
 
     void Start() {
         isMoving = false;
         showActionMenu = false;
         actionMenu = GameObject.Find("ActionMenu");
         actionMenuBox = GameObject.Find ("ActionMenuBox");
         actionMenu.SetActive(false);
         actionMenuBox.SetActive(false);
     }
 
     void Update () {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
 
         //Movement
         if(isMoving == true && showActionMenu == false) {
             rigidbody2D.velocity = positionDirection.normalized;
 
             if((positionDirection.x < 0.02f && positionDirection.x > -0.02f) &&
                (positionDirection.y < 0.02f && positionDirection.y > -0.02f)) {
                 isMoving = false;
             }
         }
 
         Xdif = positionHit.x - transform.position.x;
         Ydif = positionHit.y - transform.position.y+0.3f;
         positionDirection = new Vector2 (Xdif,Ydif);
 
         if(Physics2D.Raycast(ray.origin,ray.direction)) {
         if(Input.GetKeyDown(KeyCode.Mouse0)) {
                 positionHit = hit.point;
                 isMoving = true;    
             }
         }
 
         //ActionMenu scripting
         if(showActionMenu == true) {
             actionMenu.SetActive(true);
             actionMenuBox.SetActive(true);
         }else if(showActionMenu == false) {
             actionMenu.SetActive(false);
 
         }
 /*********************HELP IS NEEDED HERE*******************/
         if(Input.GetKeyDown(KeyCode.Mouse1)) {
             showActionMenu = true; 
             //When ActionManu is true, I SetActive my UI gameobject.
             //ActionMenu positioning over mouse should be here
         }
     }
 }

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 Victory Dan Greene · Jan 07, 2015 at 03:56 PM 0
Share

If you want the menu to appear at the mouse position, you shouldn't even need WorldToScreenPoint — the mouse position is already in Screen coordinate space. Does that help?

avatar image Stealthygolem · Jan 07, 2015 at 04:03 PM 0
Share

Hmm. Well. this is what I'm trying to do, but it fails. It only moves the giant UI element in the inspector view a tad. Because the rest of my game is very small compared to the UI canvas in the inspector:

             action$$anonymous$$enuBox.transform.position = positionHit;

$$anonymous$$y positionHit is where my mouse is clicked. This works fine on my walk-code. But, for the UI I need to scale it different somehow.

1 Reply

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

Answer by Glurth · Jan 07, 2015 at 04:10 PM

ActionMenu positioning over mouse should be here

 ActionMenu.transform.position = Input.mousePosition;

(this will place the bottom right corner of the menu at the mouse position)

WARNING: This line at the beginning, will FAIL to find the actionmenu item, if it is deactivated when this call is made. Just discovered this annoying behavior myself while testing the above.

 actionMenu = GameObject.Find("ActionMenu");

Comment
Add comment · Show 1 · 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 Stealthygolem · Jan 07, 2015 at 04:19 PM 0
Share

Awesome. This worked. Can't believe I failed to make the same connection from my previous code. But, I am still quite bad, so that's why I'm so confused at times, haha!

I am perfectly able to find the action$$anonymous$$enu item. It's all about when and how you setActives. I've done similar things in a past project without any failure yet.

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI System not blocking Raycasts on Mobile only 2 Answers

Transform.LookAt Seems To Cause Issues 1 Answer

Raycast on ui elements not working? 0 Answers

Unity 4.6 UI onClick listener doesn't work 4 Answers

Move object along ray cast using object's AddForce or velocity but not transform 1 Answer


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