Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Resolved Keep an object in the same place on the screen by moving it on the Y axis

Discussion in 'Editor & General Support' started by valter-home, Mar 28, 2023.

  1. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    72
    Hi everyone.
    In my editor I work in GameView (in edit mode) moving 3D objects with custom controls.
    I'm trying to achieve the following:

    one.png
    When I move the plane on the Y axis it obviously keeps x and z constant.

    two.png
    Instead, I would need the x and z axes to change as well so that the vertex of the plane always appears to be in the same position on the screen.

    three.png

    I tried to use
    WorldToScreenPoint 
    and then
    ScreenPointToRay 
    but without success.
    Do you have any suggestions to put me on the right track?
     
  2. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    72
    Alright, I found a satisfactory solution. Planes are meshes that I create, so I have their vertices.
    First, I store the vertices with
    WorldToScreenPoint 
    in a list.
    Code (CSharp):
    1. List<Vector3> ShapeVertices = new List<Vector3>();
    2. int Size = MyShape.Count;
    3. for (int i = 0; i <= listSize - 1; i++) {
    4.   ShapeVertices.Add(cam.WorldToScreenPoint(MyShape[i]));
    5. }
    Then I iterate again all the vertices adding or subtracting, according to the direction of the plane, a small value to Y.
    Code (CSharp):
    1. for (int i = 0; i <= listSize - 1; i++) {
    2.   Vector3 Vertices = MyShape[i];
    3.   Vertices.y += 0.1f;
    4.   // or Vertices.y -= 0.1f;
    5.   MyShape[i] = Vertices;
    6. }
    Now I create a new plane at the Y level of the modified position of the shape.
    Code (CSharp):
    1. Plane plane = new Plane(Vector3.up, new Vector3(0.0f, MyShape[0].y, 0.0f));
    And finally I use
    Raycast
    with
    ScreenPointToRay
    .
    Code (CSharp):
    1. float enter = 0.0f;
    2. Ray ray;
    3. for (int i = 0; i < ShapeVertices.Count; i++) {
    4.   ray = cam.ScreenPointToRay(ShapeVertices[i]);
    5.   if (plane.Raycast(ray, out enter)) {
    6.     MyShape[i] = ray.GetPoint(enter);
    7.   }
    8. }
    Now the new vertices are exactly where they were on the screen before changing the Y-axis.
    From here, I just have to delete the old mesh and create the new one.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,313
    Out of curiosity, what are you trying to accomplish?
     
  4. valter-home

    valter-home

    Joined:
    Sep 22, 2015
    Posts:
    72
    It's for a project I'm working on.
    it's a bit long to explain, but what I need is that the camera always frames the mesh of the same apparent starting shape and size, regardless of whether the mesh is moved on the Y axis
    Actually the third picture I posted above doesn't exactly reflect what's going on. The four vertices, by changing their position, also change the size of the mesh, and the final result, with a fixed camera, will always be like the first image.