Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Greyeeh · Jul 08, 2015 at 12:09 AM · c#uigui

Make image follow your cursor

Hello.

I've made a really simple inventory with PiDi's tutorial but I don't really know how would I nest my item icon (UI image) on specific position (let's say next to the cursor) when I'm dragging it around my inventory. Right now I'm setting the local position of my icon with this script:

 if(draggingItem)
     {
         Vector3 mousePos = (Input.mousePosition - GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>().localPosition);
         dragItemIcon.GetComponent<RectTransform>().localPosition = new Vector3(mousePos.x + 15, mousePos.y - 15, mousePos.z);
     }


The problem is that the icon is not moving as fast as the mouse and sometimes it prevents me from dropping the item to one of the slots (because I'm actually clicking at the icon image, not the slot).

It looks like this: http://i.imgur.com/zV0I1Js.gifv

I'd be very grateful for any bit of help!

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 DiegoSLTS · Jul 08, 2015 at 03:06 AM

Your code is OK, I don't think you can move the image to follow the cursor using something really different.

Maybe the problem with your code is that it's slow. I guess that code is being executed inside an Update function, so you're calling:

 GameObject.FindGameObjectWithTag("Canvas").GetComponent<RectTransform>()

and:

 dragItemIcon.GetComponent<RectTransform>()

every frame. GetComponent is not really slow, but you can save those references in a private variable instead of getting them each time. FindGameObjectWithTag IS slow, it goes through the objects on your hierarchy and checks the tag of each one until one has the "Canvas" tag. If you're always searching the same object on the scene just find it once in some Awake or Start method and use the reference in the future.

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 Greyeeh · Jul 08, 2015 at 09:33 AM 0
Share

Thank you - that worked :)

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

How to convert unity editor GUI functions to something that can be used in game? 0 Answers

How to scale triangle created by GL(using viewport position) with scale factor of Canvas? 0 Answers

How do I add something to the OnClick of a UI Button from an Editor-Script? 0 Answers

In-game text editor, native or NGUI 2 Answers

How to change the Width and Height in the script C#? (New Gui Unity 4.6 beta) 2 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