Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 supertimeal · Aug 30, 2016 at 03:13 AM · c#2dscreentoworldpoint

Problem with ScreenToWorldPoint(Input.mousePosition)

I'm making a 2d game and one mechanic is that the "player" which is just a square sprite is moved by clicking on a point on the screen. The player is then moved to that spot. No fancy moving animations just teleported to that spot. Here is my script.

 void Update ()
 {
     if (Input.GetMouseButton(0))
     {
         this.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     }
 }

I have my main camera background as white as a skybox so the player is just a black square with a white background. When I click on a spot on the screen, the player moves to the correct spot and you can see that in the scene view but in the game view, the player dissapears. Almost like it's put behind the background skybox of the camera. Can someone please help, thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by sparkzbarca · Aug 30, 2016 at 03:49 AM

the issue is your mouse on the screen is a 2d thing and the world is 3d.

your mouse can go up and down and left and right on the screen but it can never go "into" or "out of" the screen, it has a depth of zero.

As such when converting from mouse to 3d you have to determine the depth yourself.

in this case normally you'd simply copy the existing Z axis so it'd be

 transform.position = new vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y, transform.position.z);

thats not ideal, you should create a single instance of a vector3 and keep reassigning it instead of creating new variables each time probably and probably variable Camera.main.ScreenToWorldPoint(Input.mousePosition) just for readability.

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 CapitalLlama · Aug 30, 2016 at 03:55 AM -1
Share

Don't call Camera.main.ScreenToWorldPoint(Input.mousePosition) multiple times if possible. Ins$$anonymous$$d do something like this

 Vector3 _mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  transform.position = new Vector3(_mousePos.x, _mousePos.y, transform.position.z);

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Objects will not spawn in a 2d game. 0 Answers

2D Flashlight for a platformer 2 Answers

Unity Look at Mouse Position 2D C# 3 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