• 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 justking14 · May 10, 2014 at 05:08 PM · 2d3dmouseposition

2d mouseposition in a 3d world

I'm attempting to make a game where the player draws on a plane, but position the pen is giving me problems.
I've tried-
Vector3 clickedposition2 = Input.mousePosition; and the pen just flew all over the screen and in and out of my view. When I tried: Vector3 clickedposition = Camera.main.ScreenToWorldPoint(Input.mousePosition); The pen just stayed stationary. For both I changed the pen's position with: a.rigidbody.position = new Vector3(clickedposition.x, 10, clickedposition.y);

Basically, I'm just trying to get the pen to follow my mouse as it goes left/right-x direction and forward/backward-as mouse goes to top of the screen and to the bottom of the screen. Any advice?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDarkVoid · May 10, 2014 at 05:13 PM

Most of the time when you're doing something regarding screen position (0,0) is the top left of the screen, mousePosition gives you the mouse's position with (0,0) being bottom left of the screen. So you need to convert the y property by subtracting from Screen.Height.

It would look something like this:

 Vector3 rawPos = Input.mousePosition;
 Vector3 clickedposition2 = new Vector3(rawPos.x, Screen.height - rawPos.y, 0);


Comment
Add comment · Show 2 · 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 Eric5h5 · May 10, 2014 at 05:20 PM 0
Share

Input.mousePosition uses screen space. If you're using GUI space, then you must be using OnGUI code, in which case just use Event.current.mousePosition ins$$anonymous$$d. No need to convert anything.

However none of this actually has anything to do with the question.

avatar image TheDarkVoid · May 10, 2014 at 05:29 PM 0
Share

oh, I misunderstood the question...

avatar image
0

Answer by Eric5h5 · May 10, 2014 at 05:18 PM

Camera.main.ScreenToWorldPoint(Input.mousePosition) is mostly correct, but you need to supply a Z value, since otherwise it's using 0, which is at the camera (hence the "stayed stationary" thing).

Comment
Add comment · 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
0

Answer by Jeff-Kesselman · May 10, 2014 at 06:28 PM

If you want a point that is at N units in from the screen, and appears at mouse position x,y you can use

 Ray mouseray = Camera.main.ScreenToRay(Input.mousePosition);
 Vector3 point = Ray.GetPoint(<distance N>);

If you want where that pixel point is on an arbitrary surface in the 3D space, then do a ray cast with that ray against the surface in question.

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 justking14 · May 10, 2014 at 07:57 PM 0
Share

I tried that and got errors. It says there's no such thing as ScreenToRay. Did you mean ScreenPointToArray. Also, for GetPoint it says that an object reference is required to access non-static member-UnityEngine.ray.getpoint(float)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Problem with Vector3 Distance and Mouse Input, 1 Answer

Texture2D consuming too much memory 1 Answer

Drag n Drop in a 3D orthographic environment 1 Answer

2D UI in 3D space 3 Answers

Sprites: Project Sprite to Mesh, allow for bending of sprites for 3d environments 0 Answers


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