- Home /
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.
I want it to move with the mouse ins$$anonymous$$d of to the mouse, if that makes sense :)
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.
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.
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 ;)
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);
}
}
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?
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.
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.
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
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.