How to get Mouse Position in 3D and 2D! (Unity Tutorial)

Published: 23 March 2021
on channel: Code Monkey
140,208
4.2k

🌍 Get my Complete Courses! ✅ https://unitycodemonkey.com/courses
👍 Learn to make awesome games step-by-step from start to finish.
🎮 Get my Steam Games https://unitycodemonkey.com/gamebundle

💬 One of the comments I get a lot is people asking why the code shown in the video isn't working, and usually the reason is because in the video I showcase some system working in 2D and they're trying to get it to work in 3D.
It's pretty simple to get the mouse position in 3D but it is a completely different method compared to 2D.
So here let's see how both methods work and why they need to be different.
2D: Camera.main.ScreenToWorldPoint(Input.mousePosition);
3D: Physics.Raycast(Camera.main.ScreenToRay(Input.mousePosition, out RaycastHit raycastHit));

🌍 Get Code Monkey on Steam!
👍 Interactive Tutorials, Complete Games and More!
✅ https://store.steampowered.com/app/12...

If you have any questions post them in the comments and I'll do my best to answer them.

🔔 Subscribe for more Unity Tutorials    / @codemonkeyunity  

See you next time!

📍 Support on Patreon   / unitycodemonkey  
🤖 Join the Community Discord   / discord  
🎮 Grab the Game Bundle at https://unitycodemonkey.com/gameBundl...
📝 Get the Code Monkey Utilities at https://unitycodemonkey.com/utils.php

#unitytutorial #unity3d #unity2d #unity​ #gamedev​ #indiegame​ #gamedevelopment #madewithunity​ #indiedev​

--------------------------------------------------------------------

Hello and Welcome!
I'm your Code Monkey and here you will learn everything about Game Development in Unity using C#.

I've been developing games for several years with 8 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.

I do Unity Tutorials on just about every topic, Unity Tutorials for Beginners and Unity Tutorials for Advanced users.

You can see my games at www.endlessloopstudios.com

--------------------------------------------------------------------
- Other great Unity channels:
Brackeys -    / brackeys  
Dani -    / @danidev  
Jabrils -    / @jabrils  
BlackthornProd -    / @blackthornprod  
Sykoo -    / sykootv  
Jason Weimann -    / @unity3dcollege  
Jonas Tyroller -    / @jonastyroller  
--------------------------------------------------------------------

- Website: https://unitycodemonkey.com/
- Twitter:   / unitycodemonkey  
- Steam: https://store.steampowered.com/develo...


Watch video How to get Mouse Position in 3D and 2D! (Unity Tutorial) online, duration hours minute second in high quality that is uploaded to the channel Code Monkey 23 March 2021. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 140,208 times and liked it 4.2 thousand visitors.