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

Orthographic camera with rotation and ScreenToWorldPoint

I have an ortho camera which 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 this to place a graphic 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 this 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, this doesn't work and produces the same results if I left the Z axis alone.

Comment

People who like this

0 Show 0
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

Answer by lassade · Jan 26, 2018 at 05:58 PM

I think you have an complex rotation on your camera, like in the one of a isometric game, in this 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

People who like this

0 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.

avatar image

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

Ok Here is how I fixed it in my Game hope this 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 which 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 hitPoint = ray.GetPoint(enter);
             //Draw a debug ray to see where you are hitting
             Debug.DrawRay(ray.origin, ray.direction * enter, Color.green);
 
             // create a direction vector (hitPoint => somePoint
             Vector2 direction = new Vector2(
                 hitPoint.x - somePoint.transform.position.x,
                 hitPoint.y - somePoint.transform.position.y
                 );
 
             // addforce force to the projectiles rigidbody in that direction.
             projectile.AddForce(direction * projectileSpeed);
 
         }
 
     }
 

Hope this clears it up a bit. This 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
mchts
FPGLimited

People who like this

2 Show 0 · 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.

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

Can I simulate an object from 30000 meters away? 1 Answer

How to prevent continuous spinning while aiming at the screen's center? 0 Answers

Unity5 scene camera clips planes 0 Answers

An object who stay in the camera area 1 Answer

Main Camera XYPoint To Background Camera XYPoint 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