Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved How to get world space position with a fixed y value

Discussion in 'Scripting' started by RealMcCoyGames, Jul 7, 2021.

  1. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    So I need y to always equal 2.

    How can I get a position in world space, relative to the camera, but where y always equals 2.

    So if you imagine clicking somewhere to get a point in world space. But where y is 2.

    Like there is an invisible plane at y:2 and you click to get a a specific point on that plane.

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    RealMcCoyGames likes this.
  3. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    Thanks! Yea I think Plane.Raycast is what I need.

    So if I set up the plane along the x and z axis and at y:2,
    then shoot a ray out from the camera,
    and get the point where the ray intersects with the plane.
     
  4. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    I've managed to get it doing what I want but it's in the wrong spot. Seems like the y value is 0 when I need it at 2.

    Think it's to do with how I'm creating the plane, I think I'm doing it wrong. Looked in the documentation but it's not always easy to understand.

    My code:

    Code (CSharp):
    1. public class RaycastToPlane : MonoBehaviour {
    2.  
    3.     public Camera sceneCamera;
    4.     public GameObject cube;
    5.     Plane plane;
    6.    
    7.  
    8.     void Start() {
    9.         plane = new Plane(new Vector3(0, 2, 0), Vector3.zero);
    10.     }
    11.  
    12.     void Update() {
    13.  
    14.         Vector3 pos = sceneCamera.transform.position;
    15.          Vector3 dir = (this.transform.position - sceneCamera.transform.position).normalized;
    16.  
    17.         var ray = new Ray(sceneCamera.transform.position, dir);
    18.  
    19.         if(plane.Raycast(ray, out var distance)) {
    20.  
    21.             var hitPoint = ray.GetPoint(distance);
    22.  
    23.             Debug.Log(hitPoint);
    24.  
    25.             cube.transform.position = hitPoint;
    26.  
    27.         }
    28.        
    29.     }
    30.  
    31. }
     
  5. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    OK I had the direction and position mixed up when spawning the plane. It should have been:

    Code (CSharp):
    1. plane = new Plane(Vector3.up, new Vector3(0, 2, 0));
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Correct!

    Because it takes two identical arguments, whenever I call a function (or constructor) like that, I always like to give named arguments to avoid mismatching.

    Instead of this:

    Code (csharp):
    1. plane = new Plane(Vector3.up, new Vector3(0, 2, 0));
    Go the extra mile and name them:

    Code (csharp):
    1. plane = new Plane( inNormal: Vector3.up, inPoint: new Vector3(0, 2, 0));
    Same thing goes for nasty overloads like Physics.Raycast()... in fact, I always give all named arguments to any flavor of Raycast(), since the LayerMask integer can be confused for MaxDistance.
     
    RealMcCoyGames likes this.
  7. RealMcCoyGames

    RealMcCoyGames

    Joined:
    Jul 23, 2018
    Posts:
    65
    I think I'll start doing that, sounds like a good practice!

    Thanks again for your help!
     
    Kurt-Dekker likes this.