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 AndreasX12 · Sep 02, 2014 at 03:16 PM · inputpositionmouseguitexture

Move GUITexture with mouse

I know this is a very easy question.

This is my code when mouse is down:

transform.position = new Vector3(0.5f,(Input.mousePosition.y / Screen.height), 3);

I'm using transform.position to move the GUITexture. However, my problem is that whenever I select/click the GUITexture, it moves directly to the mouse position. How can I make it not move when I click it? Please let me know if this is bad explained.

Comment
Add comment · Show 3
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 kacyesp · Sep 02, 2014 at 03:18 PM 0
Share

And what behaviour are you ai$$anonymous$$g for?

avatar image AndreasX12 · Sep 02, 2014 at 03:19 PM 0
Share

I want it to move with the mouse ins$$anonymous$$d of to the mouse, if that makes sense :)

avatar image AndreasX12 · Sep 02, 2014 at 03:22 PM 0
Share

When I click the GUITexture, it goes directly to the y center of my mouse. But I want to click anywhere on the GUITexture and move it from where I clicked ins$$anonymous$$d of from the center.

2 Replies

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

Answer by robertbu · Sep 02, 2014 at 06:45 PM

You did not share the rest of your code, so I have to guess a bit. You are likely executing the line of code in your question in OnMouseDrag(). The 'trick' is to capture the offset between the mouse and your object when the mouse goes button goes down. If you are using OnMouseDrag(), then do it in OnMouseDown(). If you are using Input.GetMouseButton(), then do it in Input.GetMouseButtonDown(). You calculate the pixel offset from your mouse position to the screen coordinate of your GUI.Texture.

  Vector3 offset = Camera.main.ViewportToScreenPoint(transform.position) - Input.mousePosition;

You will add this offset to Input.mousePosition and use the result in your line of code above.

Comment
Add comment · Show 5 · 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 AndreasX12 · Sep 02, 2014 at 09:22 PM 0
Share

Thank you for your answer, robertbu. Here is my full code with your code integrated (The only thing that is missing in the code sample below is the line that defines the gui2_contain bool):

 void Update () {
 
     if(Input.Get$$anonymous$$ouseButton(0) && gui2_contain == true) {
 
         Vector3 offset = Camera.main.ViewportToScreenPoint(transform.position) - Input.mousePosition;
             
         transform.position = new Vector3(0.5f,(Input.mousePosition.y + offset.y) / Screen.height, 3);
 
     }
 
 }

Now it wont move at all ;)

avatar image robertbu · Sep 03, 2014 at 09:13 AM 1
Share

As mentioned in my answer, you have to only get the offset on the mouse down:

 void Update () {

     if (Input.Get$$anonymous$$ouseButtonDown(0) && gui2_contain) {
         Vector3 offset = Camera.main.ViewportToScreenPoint(transform.position) - Input.mousePosition;
     }
 
     if (Input.Get$$anonymous$$ouseButton(0) && gui2_contain) {
           transform.position = new Vector3(0.5f,(Input.mousePosition.y + offset.y) / Screen.height, 3);
     }
 }
avatar image AndreasX12 · Sep 05, 2014 at 09:06 PM 0
Share

Hi again Robertbu. I have tried to figure it out the last couple of days, but somehow I can not get it to work.

         if(Input.Get$$anonymous$$ouseButtonDown(0)) {
 
             if(showInfoBackground.guiTexture.HitTest(Input.mousePosition) || transform.guiTexture.HitTest(Input.mousePosition)) {
 
                 gui2_contain = true;
                 offset = Camera.main.ViewportToScreenPoint(showInfoBackground.transform.position) - Input.mousePosition;
                 
             }
             
         }
         
         if(Input.Get$$anonymous$$ouseButton(0) && gui2_contain == true) {
 
             transform.position = new Vector3(0.5f,(Input.mousePosition.y + offset.y) / Screen.height, 3);
 
         }


This is my current code. I think the problem might be, that the GUI I want to move consists of 2 GUITextures. One caled 'showInfoBackground' which is a child of the second one called 'transform'. The reason it's just 'transform' is, that I have attached the script to the parent GUITexture.

Can you (or anyone else) see where the problem is?

avatar image robertbu · Sep 06, 2014 at 02:00 AM 1
Share

I'm out of town with access to Unity, so I cannot test your setup. But when I have problems like this one, I take a step back, start a new scene, and create the simplest setup I can. In your case (assu$$anonymous$$g you are not running 4.6), create a GUITexture, and add your movement script (ether whole script or cut-down logic). Do not replace the texture. Use the default one. This will assure that the problem is not the pixel inset values.

avatar image AndreasX12 · Sep 06, 2014 at 10:18 AM 0
Share

Thank you very much Robertbu! :) I got it working now. Found out that I had changed something so the offset wasn't relative to the GUITexture.

avatar image
0

Answer by kacyesp · Sep 02, 2014 at 03:27 PM

It seems this person nearly has the same question. You could use the code in the answer: http://answers.unity3d.com/questions/350220/how-do-i-drag-a-guibutton-.html

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 AndreasX12 · Sep 02, 2014 at 06:10 PM 0
Share

Thanks for your answer. I know how to move the GUITexture, but when I click the GUITexture it instantly position itself at the center of the mouse. I want to be able to drag the GUITexture without having it locked at the center of the mouse when moving the GUITexture.

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

23 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

Related Questions

Word or string position under mouse position 1 Answer

Can I fake the mouse/touch position relative to the real mouse/touch position? 0 Answers

Need help on the 3ds max style camera control 0 Answers

Input.mousePosition returns coordinates as integers. 1 Answer

Move to mouse position 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