• 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 doddsey65_unity · Jan 26, 2018 at 03:35 PM · camerascreentoworldpointclipping plane

Orthographic camera with rotation and ScreenToWorldPoint

I have an ortho camera w$$anonymous$$ch used to have no rotation. When I used

 ScreenToWorldPoint(Input.mousePosition)

I would get the desired result. I have a grid of tiles and I would use t$$anonymous$$s to place a grap$$anonymous$$c over the tile that was under the mouse.

However, now that I have added some rotation to the camera, the selected tile isn't actually the one that is under the mouse. I realise that t$$anonymous$$s is to do with rotation and some answers I have already looked at suggest setting the Z to the cameras near clipping plane like so:

 Vector 3 mousePosition = Input.mousePosition;
 mousePosition.z = Camera.main.nearClipPlane;
 Vector3 position = Camera.main.ScreenToWorldPoint(mousePosition);

However, t$$anonymous$$s doesn't work and produces the same results if I left the Z axis alone.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by SteenPetersen · Feb 05, 2018 at 04:29 PM

Ok Here is how I fixed it in my Game hope t$$anonymous$$s helps. Lassade was correct btw here is how you do it:

You need to spin up a plane so that you can cast a ray at it in order to determine what the distance is from the camera.

So in my game I have a -30 rotation on the X of the camera w$$anonymous$$ch is ultimately means the 'world' is further from the top of the screen than it is from the bottom. That means that if I want to shoot a projectile say at the mouse. I need to know the exact mouse click position.

alt text

What I did was spin up a Plane in the same position as my world and cast a ray at it to determine what distance it was from the camera.

 // define a plane variable
 // may be Vector3.up in your case depending on your camera orientation
 Plane plane = new Plane(Vector3.forward, Vector3.zero);
 
 // Shoot a ray at it when your event happens, in my case projectile
 
 public void OnCastComplete()
     {
 var projectile = Instantiate(projectile, somePoint.transform.position, transform.rotation);
 
         //Create a ray from the Mouse click position
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         //Initialise the enter variable
         float enter = 0.0f;
 
         if (plane.Raycast(ray, out enter))
         {
             Debug.Log("distance " + enter);
             //Get the point that is clicked
             Vector3 $$anonymous$$tPoint = ray.GetPoint(enter);
             //Draw a debug ray to see where you are $$anonymous$$tting
             Debug.DrawRay(ray.origin, ray.direction * enter, Color.green);
 
             // create a direction vector ($$anonymous$$tPoint => somePoint
             Vector2 direction = new Vector2(
                 $$anonymous$$tPoint.x - somePoint.transform.position.x,
                 $$anonymous$$tPoint.y - somePoint.transform.position.y
                 );
 
             // addforce force to the projectiles rigidbody in that direction.
             projectile.AddForce(direction * projectileSpeed);
 
         }
 
     }
 

Hope t$$anonymous$$s clears it up a bit. T$$anonymous$$s will certainly help in my game and it took me a long time to find the solution so I hope it helps others.


camera-explain-1.png (29.1 kB)
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 lassade · Jan 26, 2018 at 05:58 PM

I t$$anonymous$$nk you have an complex rotation on your camera, like in the one of a isometric game, in t$$anonymous$$s case you should convert your sceen point to ray then raycast it on a plane (look at Plane class) or in the collider geomtry of your game.

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 doddsey65_unity · Feb 03, 2018 at 07:19 PM 0
Share

Thanks for the info. The game has no colliders, each tile is just a sprite.

avatar image SteenPetersen · Feb 05, 2018 at 03:41 PM 0
Share

I have a very similar problem and cannot seem to fix it, trying to work on it at the moment will update if I find a solution.

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

121 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 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 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 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 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

Need explanation for this code 1 Answer

How to have player camera Render enemy but not itself with same tag and camera clipping mask? 0 Answers

Need help putting an object at a point based on screen coordinates 1 Answer

mouse position on terrain 1 Answer

Shadows on multiple cameras 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