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
1
Question by Bruno Gysin · Jan 25, 2015 at 06:16 AM · uitransformrect

Set Position UI By Mouse Position

Hi,

I need to put a UI image following the mouse. 'the UI image center stay in the point of the mouse.' I used (Input.mousePosition) but it is not exactly the mouse pointer. anyone have any ideas for it?

Comment
Add comment · Show 1
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 getyour411 · Jan 25, 2015 at 06:17 AM 0
Share

Input.mousePosition is the mouse pointer, maybe your code is needing a conversion to a different coordinate system; show your code

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by curiouspers · Jan 25, 2015 at 02:59 PM

I think that one should help.

https://unity3d.com/ru/learn/tutorials/modules/intermediate/live-training-archive/panels-panes-windows

take a look at DragPanel example and anchoredPosition and anchoredPosition3D, use it like this:

 image.rectTransform.anchoredPosition3D = new Vector3(my_new_x, my_new_y, my_new_z);

Comment
Add comment · Show 2 · 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 Bruno Gysin · Jan 25, 2015 at 03:53 PM 0
Share

is not exactly the mouse pointer:

GetComponent(RectTransform).anchoredPosition3D = Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);

alt text

avatar image curiouspers · Mar 03, 2015 at 07:33 AM 0
Share

sorry, maybe this is too late, but perhaps you need to use Camera.ScreenToWorldPoint OR Camera.ScreenPointToRay and Physics.Raycast in conjunction with anchoredPosition3D, documentation has good example

avatar image
1

Answer by cdr9042 · Nov 05, 2020 at 03:40 AM

RectTransformUtility.ScreenPointToLocalPointInRectangle can help you

ScreenPointToLocalPointInRectangle(RectTransform rect, Vector2 screenPoint, Camera cam, out Vector2 localPoint);

rect: the parent RectTransform of the UI object you're moving.

screenPoint: your mouse position in screen space

Camera: the camera your Canvas is rendered on

localPoint: set the anchored position of the UI object to this vector

Example:

 RectTransform movingUI;
 RectTransform parentRect = movingUI.parent as RectTransform;
 ScreenPointToLocalPointInRectangle(parentRect, mousePosition, Camera.main, out Vector2 localPoint);
 movingUI.anchoredPosition = localPoint;
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change size of the new UI rect transform using scripts 0 Answers

Convert Input.mousePosition to RectTransform pivot position 2 Answers

How to change the Top and Bottom (rect.yMin and yMax) properties of a rectTransform, in a script? 2 Answers

[Unity 4.6 Beta] Rect Transform Position (New UI System) 7 Answers

Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 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