Unity C# - How to get the mouse position

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good day everyone now here's a quick video of how to get the mouse position here from the screen into your script now okay if i hide this canvas because it's purely just for demonstration purposes starting with a new scene we will create a script and you can call it anything you want but i'm going to call it mouse position and then i'm going to create an empty game object called mouse i just need somewhere to put the script for now so i'm going to attach that to my mouse game object and you can see it appears there so let's open this one up and we're only going to need the update function here for this one and by placing it in the update we'll get a new value each frame so to start off we'll create a vector3 call it mouse position and then to get that we do input.mouse position and this here will return a vector 3 for ease of use when interacting with the 3d game world however the z value of this vector will always be zero now i think i would prefer a vector2 myself as the screen is represented in two dimensions but it's not a big deal okay so the quickest way to access the coordinate values of this will be to create a float we'll call it x and we'll go mouse position dot x and then we'll do the same for y now it really is that quick to get the values and now you can use them however you want so i'll save that and for this demonstration i have made some canvas elements so if i turn these canvas elements on here we go and i click play and as you can see here these values are what's being output by the input.mouse position now if we move our mouse around a bit you can see that here is our origin of our game view so if i move my mouse all the way here you get 0 0 but the values will continue to update even if i go outside the game window as you can see now this will normally happen if your cursor doesn't have a lock state and even the values will continue across multiple monitors and be careful when you're playing with the scale here as you will get funky values being returned now a minus value would generally mean that our cursor has left the game view in at least three different quadrants here so that can be helpful keep in mind when you do set an aspect ratio the origin is now here as the game view is now here the game window is not counted towards the mouse position like a game view area as you'll see the minus values now cool so i hope this explanation was helpful thank you for watching and there's a snippet of the code in the description if you would like to see it and i'll see you next time
Info
Channel: Danndx
Views: 7,429
Rating: undefined out of 5
Keywords: unity c# mouse, unity mouse position, unity c# mouse position, unity mouse c#, unity mouse script, unity c# cursor, unity cursor position
Id: 5VaV7W20rsk
Channel Id: undefined
Length: 3min 0sec (180 seconds)
Published: Wed Apr 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.