Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SarperS · Nov 05, 2014 at 10:01 PM · camerascreentoworldpointtransformation

Projecting Oblique Screen Coords to Straight World Coords

I'm working on a game featuring a tilted perspective camera. For some purposes I must find the world coords of the mouse position but I want the returned world coords in a fixed height. Currently I'm using this code (pseudo)

 ScreenToWorldPoint(mousePosition.x, mousePosition.y, nearClipPlane + distance)

As the camera is tilted, it returns different Y positions which ruins the output for me. Please take a look at the screenshot provided below and let me know how I can find the position that exactly matches the mouse position in the fixed height straight plane. (note: the game is perspective 3D, don't let the screenshot make it look like ortho 2D)

Thanks in advance!

P.S. Using raycasting is not an option, this should ideally work solely mathematically

alt text

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 troien · Nov 05, 2014 at 10:20 PM

If your 'ground' is a gameobject with a collider, you can simple raycast. Using Physics.Raycast

If your 'ground' is simply a fixed y position. Then the following piece of code should work if you add it to your Camera. (note that you should be pointing your camera at the ground though, otherwise the position inverts and ends up behind the camera)

 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     private void OnDrawGizmos()
     {
         Ray ray = camera.ScreenPointToRay(Input.mousePosition); // This ray could be used for raycasting aswell
         Vector3 positionOnFloor = ray.origin + (ray.direction * ((ray.origin.y - 0) / -ray.direction.y)); // the 0 in this calculation is the height (in world position) of your floor
         Gizmos.DrawWireSphere(positionOnFloor, 1f); // As example, I draw a gizmo on the floor
     }
 }


Comment
Add comment · Show 3 · 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 SarperS · Nov 05, 2014 at 10:38 PM 0
Share

Amazing, thank you so much for the fast response! If you have time could you please describe what's going on in the below line so I can learn? Thanks!

ray.origin + (ray.direction * ((ray.origin.y - 0) / -ray.direction.y));

avatar image troien · Nov 06, 2014 at 01:00 PM 1
Share

I'm having problems with finding a good way to explain this. So I decided that I just make a image :p I hope this explains it.

Important thing to note: The direction is usually normalized. But not in this example because it makes calculating by head more difficult. Results would still be equal though so shouldn't be a problem.

alt text

maths.png (62.9 kB)
avatar image SarperS · Nov 06, 2014 at 02:33 PM 0
Share

Perfect, thank you so much for the detailed explanation!

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

Need explanation for this code 1 Answer

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

mouse position on terrain 1 Answer

Indicate with markers the direction of Gameobjects which are outside of the camerafield. 0 Answers

Orthographic camera with rotation and ScreenToWorldPoint 2 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