Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Suraj. · Aug 13, 2015 at 05:22 AM · 2d gamemousepositionperspective cameracamera.screentoworld

Camera.main.ScreenToWorldPoint in Perspective Camera

Hi. I have researched a lot and saw similar posts but I am not able to fix my problem. Any kind of help would really be appreciated. So I am developing a 2D game. Have touch ,Everything working perfectly fine. However I have decided to change camera to perspective for game. which is giving a perfect look to game. currently camera position is 0,-6.27,-6.32 and rotation 320,0,0.

My only problem is that my touch is not working now. I understand its due to z position problem for perspective camera. But tried everything but i am just not able to fix this. please help...

Comment
Add comment · Show 2
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 fafase · Aug 13, 2015 at 06:05 AM 0
Share

What is not working?

avatar image Suraj. · Aug 13, 2015 at 08:03 AM 0
Share

touch is not working. Lets say Input.mouse is also not working. Its because Camera.main.ScreenToWorldPoint can be used only for orthographic camera. And I want to use perspective cam. example below will work for orthocam perfectly.but not for perspective

Lets say ...

  currTouchpos = Input.GetTouch(i).position;
 
 //and now in Update()
     
     Vector3 mouseRay = Camera.main.ScreenToWorldPoint(currTouchpos);
             Vector2 touchPos = new Vector2(mouseRay.x, mouseRay.y);
     if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touchPos)
     {
     //Do something
     }



4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by koushik.s · Aug 13, 2015 at 08:23 AM

You can use Raycast for detect the collision of the object

Try this code may help you Vector3 mousePosition = Input.mousePosition; mousePosition.z = 10f; RaycastHit2D hit;

         Vector2 v = Camera.main.ScreenToWorldPoint(mousePosition);
         
         Collider2D[] col = Physics2D.OverlapPointAll(v);
         
         if(col.Length > 0){
             foreach(Collider2D c in col)
             {
                 Debug.Log("Collided with: " + c.collider2D.gameObject.name);
             
             }
         }
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 Suraj. · Aug 13, 2015 at 09:04 AM 0
Share

Thanks..But I tried my friend..But its not working. You see its 2D game and perspective camera. Consider my above comment as my code...

avatar image Suraj. · Aug 14, 2015 at 06:44 AM 0
Share

Hi koushik. Thanks and your code kind of seems to be working. When I hit gameobject debug gives around 8-80 logs for just one click. Basically I click on one gameobject and it actually click other gameobjects with same name on screen too.(I did Destroy(this.gameobject) on click.And found out when i click on one gameobject other objects gets detroyed too) I think I need to tell that my perspective camera is rotated. so using mousePosition.z = 10f is not gonna work right>?? Also we not using RacastHIt2D anywhere.

avatar image
0
Wiki

Answer by Nabeel Akhtar · Aug 13, 2015 at 09:14 AM

I had the same issue and this pattern solved my problem. Hope this helps u out :)

if (Input.touchCount > 0 && Input.GetTouch(0).phase != TouchPhase.Ended) {

         touch = Input.GetTouch(0);
         
         Vector2 pos = new Vector2(touch.position.x , touch.position.y);
         Vector2  newPos = Camera.main.ScreenToWorldPoint(pos);
         x = newPos.x;
         y = newPos.y;
         
     }
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 Suraj. · Aug 14, 2015 at 06:36 AM 0
Share

Thank you. But this wouldnt work. And also I have multiple touch in my game.

avatar image
0

Answer by Suraj. · Aug 17, 2015 at 07:23 AM

OK. Thanks all for trying to help. BUt the only solution to this problem was to change colliders of gamobjects to 3d. and use physics.raycasthit instead of Camera.main.ScreenToWorldPoint.

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 chariot · Aug 17, 2015 at 07:30 AM

For God's sake, why u use Camera.main.ScreenToWorldPoint(pos);? Use var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);

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

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

27 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 avatar image avatar image avatar image avatar image

Related Questions

Bullet speed changes depending on how close and far I click the mouse button 2 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Tryna get the position of the mouse in world space 1 Answer

Unity 2D Shoot towards mouse position 1 Answer

Cannot get mouse position in 2D using perspective 1 Answer


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