GUI Element to Mouse Position

I can never seem to get this to work. I’m creating a tooltip and I want it positioned EXACTLY below the mouse (courser) position on the screen. I want the tooltip element to be SetActive(true) when the mouse is hoovering over another GUI element that has a description. Can somebody point me in the right direction? By the way I’m using the 4.6 GUI system.

To make something follow the mouse position:

//calculating mouse position
var MousePosition = Input.mousePosition;
MousePosition = Camera.main.ScreenToWorldPoint(MousePosition);
//as you're using GUI elements for this, you may wanna limit or set specific value for your Z axis, here you go
MousePosition.z = valueHere;

//making it follow the mouse
transform.position = Vector3.MoveTowards(transform.position, MousePosition, speedOfMovementHere);

Replace “valueHere” about how much in-depth the Z axis will be, and replace “speedOfMovementHere” with how fast you want your object to follow the mouse position (number on both fields, can also be used with variables (obviously)).

Okay, but why are you assigning the MousePosition variable two times in two different ways?

I do not assign them twice. If you wonder the “MousePosition = Camera.main.ScreenToWorldPoint(MousePosition)”, it’s basically reading the MousePosition variable, which is obviously the position of the mouse as I’ve assigned it in the code above (var MousePosition…). Then ScreenToWorldPoint basically tells it to read only the available screen border, which makes it non-buggy. If that’s what you asked ? :slight_smile:

Okay. I think the confusion is the UnityScript vs C# (for me I code in C#).

The code I sent is in C# so you should be able to use it :slight_smile: Be sure to put it inside of your update method!

C# doesn’t use “var” to declare variables. C# identifies variables based on what they are. For example:

private Vector3 MousePos;

void Update(){
MousePos = Input.mousePosition;
}

Basically, var can be used in C# as well inside any function.

Right…that’s what I’m saying. In C# you have to state what a variable will hold (strings, integers, booleans, etc.). You can’t just create it and assign it (which is why C# doesn’t use “var”), without declaring what it is.

The code I wrote from the beginning works. It’s just that we’re saying that “var MousePos” is “Input.mousePosition”. That means we’re already defining it to be a Vector. So if we said “MousePos = 5” it’d give an error. But if we wrote "var MousePos = “2"” then wrote “MousePos = 2” it would work becuase MousePos is defined then to a integer. So basically, you can write var… without using any other variables in general in C# too, but it’s just alot more different than JavaScript/UnityScript.

The picture I posted says the same thing I mentioned recently. You can not define the var to be a string and then change it to any integer or float. In the end, you don’t have to define the variable in C# literally like you useally do, by writing like you did “private Vector3 mousePos”.

That’s actually not true :slight_smile: http://msdn.microsoft.com/en-us/library/bb383973.aspx

That being said - I’m not sure why it’s being suggested to position GUI elements using a screen space to world space transformation. Input.mousePosition is already in screen space which is what you want for GUI. The key difference is that you need to invert the Y axis before positioning your GUI element (y = Screen.height - Input.mousePosition.y). This is done because GUI vertical space starts at the top of the screen (y=0 at top) whereas screen space starts at the bottom (y=0 at bottom)

Hmmm. Clad you posted that link Kelso. I (evidently) had a misconception about implicit and explicit when it comes to C# variables.

It’s handy to avoid dumb stuff like this

private Dictionary<string, List<GameObject>> lookup = new Dictionary<string, List<GameObject>>();

Instead

var lookup = new Dictionary<string, List<GameObject>>();

But of course you’ll get people who do this kind of nonsense

var thing = something.GetTheOtherThing();

Which forces everyone who isn’t the person that wrote it to go look at the method signature to see what gets returned.

And it’s worth mentioning that you can’t do this

var lookup;

lookup = new Dictionary<string, List<GameObject>>();

Declaration and assignation needs to be together.

Thank you for descirbing this in-depth, I was myself not 100% sure about how all the rules looked like and now I got a bit more experience with it :slight_smile: