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
6
Question by Gennadios · Jun 19, 2012 at 02:49 AM · vector3ray

Ray: finding out x and z coordinates where it intersects y = 0

Hello, I'm trying to do an RTS style building placement where the blueprint of an object follows the mouse while dragging along the ground.

The catch is that this is that I'm effectively placing the terrain, there may or may not be something in that location, so collision detection isn't really an option.

I need a reasonably simple way to find out where my ScreenPointToRay ray intersects y=0 but my vector math is rusty to non-existant. Is there a function or set of functions in Unity that would make this simpler than breaking down the two vectors I have into individual numbers and coding a wall of geometry?

what I currently have:

 void Update()
 {
 Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
 
 ???
 
 temp.transform.position = new Vector3((int)???,0,(int)???);
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
26
Best Answer

Answer by aldonaletto · Jun 19, 2012 at 03:10 AM

You can create a horizontal logic plane at y=0, then use a Plane.Raycast to find where the ray hits the plane and get the exact position with Ray.GetPoint:

 void Update(){
   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   // create a plane at 0,0,0 whose normal points to +Y:
   Plane hPlane = new Plane(Vector3.up, Vector3.zero);
   // Plane.Raycast stores the distance from ray.origin to the hit point in this variable:
   float distance = 0; 
   // if the ray hits the plane...
   if (hPlane.Raycast(ray, out distance)){
     // get the hit point:
     temp.transform.position = ray.GetPoint(distance);
   }
 }
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 Gennadios · Jun 19, 2012 at 03:29 AM 0
Share

That is incredibly elegant and does exactly what I was ai$$anonymous$$g for, thank you.

avatar image Wolfram · Jun 19, 2012 at 10:05 AM 0
Share

nice answer!

avatar image Buey · May 30, 2013 at 07:03 AM 0
Share

great answer, thank you

avatar image JayCrossler · Aug 02, 2013 at 05:47 PM 2
Share

Thanks, works great with Touch as well. Here's how I did it:

     void Update(){        
         if(Input.touchCount > 0) {
             Touch touch = Input.GetTouch(0);
             switch(Input.GetTouch(0).phase) {
                 case TouchPhase.Began:    
                     $$anonymous$$akeSphere(touch);
                     break;
                 case TouchPhase.$$anonymous$$oved:    
                     break;
                 case TouchPhase.Ended:    
                     break;
             }
         }
     }
     void $$anonymous$$akeSphere(Touch touch){
       Ray ray = Camera.main.ScreenPointToRay(touch.position);
       // create a plane at 0,0,0 whose normal points to +Y:
       Plane hPlane = new Plane(Vector3.up, Vector3.zero);
       // if the ray hits the plane...
       float distance = 0; 
       if (hPlane.Raycast(ray, out distance)){
         // get the hit point:
         Vector3 location = ray.GetPoint(distance);
             
         GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
         sphere.transform.localScale*=5;
         sphere.transform.position = location;
       }
     }
avatar image
2

Answer by Wolfram · Jun 19, 2012 at 02:58 AM

If it's really just y=0 you are interested in, make a plane with collider that covers your whole area, remove the MeshRenderer, so that it's an invisible collider, and place it at y=0.

Then you can simply raycast at this particular collider, using Collider.Raycast(...), and it will always return the intersection with y=0 at that point.

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 Gennadios · Jun 19, 2012 at 03:28 AM 0
Share

I had just finished juryrigging a plane to follow underneath the camera when Aldo's answer came up. Both answers gave the same result but his is more scalable. It appears I can't flag multiple answers though, so his has to be it=(

avatar image
0

Answer by randomdragon · Jan 24 at 11:39 AM

Knowing the x1, to get the y1 and z1, simply use the direction vector v= ( x, y, z) :

dx = x1 - x;

dy = v.y * dx/v.x;

dz = v.z * dx/v.x;

y1 = y + dy;

z1 = z + dz;

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What happens when I populate a ray with a vector2? 1 Answer

Casting a Ray in the direction of the movement 1 Answer

How do I keep functionality of LookAt script whilst using it in a 3dGUI camera layer? 0 Answers

Question about Rays and why getPoint function 1 Answer

Math Question : How to Get a Point on a Ray which is EXACTLY 5 units away from Vector3.Zero? 5 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