Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
4
Question by ExusAltimus · Mar 27, 2015 at 09:25 PM · raycastpositionscreenplaneworld

Screen point to point on Plane WITHOUT Raycast?

How do I use the position taken from Input.mousePosition, take a gameobject (which is a plane), and calculate the local space point on the plane (or world point on the plane doesn't matter)? I can see why raycast would be useful with multiple gameobjects, but I only have one and I'm sure there is another way without using it, whether it be built in or an algorithm. Thanks for your help.

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
7

Answer by jpthek9 · Mar 27, 2015 at 09:44 PM

The trickiest part is converting a 2d point on the screen into a 3d direction. Luckily, Unity is kind enough to provide you a nifty algorithm to do this: Camera.ScreenPointToRay which will give you a Ray, essentially a start point (the camera's position) and a direction. Assuming that your plane is horizontally flat, all you have to do is solve a linear equation.

You can find the delta between the ray's start point's y position and the plane's y position and multiply that by the direction of the ray to find how much the position as changed. Then normalize the direction by its y position and... Okay, this is getting kind of confusing to. Here's an example:

 Plane plane = new Plane();
 Camera camera = new Camera();
 void Start()
 {
 plane.transform.position = Vector3.zero;
 camera.transform.position = Vector3.up * 15;
 }
 
 void Update()
 {
 if (Input.GetMouseButtonDown(0))
 {
 Nuke (GetIntersectionPos (plane, camera));
 }
 }
 
 Vector3 GetPlaneIntersection(Plane plane, Camera camera)
 {
 Ray ray = camera.ScreenPointToRay(Input.MousePosition);
 float delta = ray.origin.y - plane.transform.position.y;
 Vector3 dirNorm = ray.direction / ray.direction.y;
 Vector3 IntersectionPos = ray.origin - dirNorm * delta;
 return IntersectionPos;
 }
Comment
Add comment · Show 4 · 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 graciasluigi · Mar 14, 2016 at 01:54 PM 1
Share

Awesome! Could you tell me where the concept came from? I want to formalize the knowledge..

avatar image jpthek9 graciasluigi · Mar 14, 2016 at 03:40 PM 1
Share

Algebra 2.

avatar image graciasluigi jpthek9 · Mar 14, 2016 at 05:37 PM 1
Share

Again, thank you for the very good answer, it was hard to find it.

About the concept, could you be more specific? I am putting debug draw lines so I can understand what really is going on, but not getting it yet.

This is the main confusion for me. Vector3 dirNorm = ray.direction / ray.direction.y;

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Keeping the player inside the screen? 2 Answers

Performance issue Raycast each frame 1 Answer

World to Screen 1 Answer

UI RectTransform Position && Screen Resolution 2 Answers

Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 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