Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Create Lines/Paths in 2d program and then export to 3d program

Discussion in '2D' started by unity_9C31F72053387C314EDE, May 22, 2021.

  1. unity_9C31F72053387C314EDE

    unity_9C31F72053387C314EDE

    Joined:
    May 20, 2021
    Posts:
    16
    I'm developing a VR "game" in 3D, and I have a separate 2D unity project for it.

    In the 2D project I want to be able to create Paths (Lines with linerenderer). I then want to be able to export these paths to a file and add it to the 3D Game so that the players in the 3D game can follow these paths on a flat surface in a 3D space (In other words: The path has two dimensions, no player is ever going up and down).

    Question is: How do I make sure that the position, length, path of the line will be the same for the 3D game. If I use a plane in the 3D game, do I also have to use a plane in the 2D program? Or can I use a grid in the 2D program?



    I'm essentially trying to make the lines "compatible" between 2D and 3D.
     
  2. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    107
    Y-Hello there,

    Do you really need to have a separate 2D project just to create a path? You could just do it in the 3D one.

    Anyhow, the line renderer path is just a collection of vectors, eg. points in space. If you want to "export" them, you'd have to serialize the array in a Json, and deserialize the file onto the other prj.

    That won't be a problem if you use the same unit scale. (Unity as convention is 1 unit as 1 mt).
    So for example creating an array of Vector3 we can pass then the positions we want just on the X, and Z axis.
    The current positions are of 1 Unit length, and will form a square, if you keep the same scale between the 2 project you wont have any problem.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LineCreation : MonoBehaviour
    6. {
    7.     public Vector3[] positions = { new Vector3 { x = 0, y = 0, z = 0 },
    8.                             new Vector3 { x = 0, y = 0, z = 1},
    9.                             new Vector3 { x = 1, y = 0, z = 1},
    10.                             new Vector3 { x = 1, y = 0, z = 0},
    11.                             new Vector3 { x = 0, y = 0, z = 0}};
    12.  
    13.     public LineRenderer lineRend;
    14.  
    15.     [ContextMenu("Set Positions")]
    16.     private void LineRendererWithPositions()
    17.     {
    18.         lineRend.positionCount = positions.Length;
    19.         lineRend.SetPositions(positions);//
    20.     }
    21. }
    22.  


    What you need to do is now to export that
    Vector3[] positions
    as Json, import it and set the LineRenderer Positions.

    Hope this helps :)
     
  3. unity_9C31F72053387C314EDE

    unity_9C31F72053387C314EDE

    Joined:
    May 20, 2021
    Posts:
    16
    That is a detailed answer my man thanks for your time!
    The reason why I do this is because in the 2D project I am creating whole (american) football plays, which would be a pain in the ass with the oculus quest (2).

    An as far as using a grid, plane or only the camera I dont have to worry right? Just the unit has to be the same and then I have to export the vectors with json. Thanks!
     
  4. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    107
    Np :D

    As long as you keep you planar space (eg. using (X,Y), (X,Z), or (Y,Z)) and Unit length the same across the 2 projects you shouldn't have trouble.

    Happy coding :)
     
  5. unity_9C31F72053387C314EDE

    unity_9C31F72053387C314EDE

    Joined:
    May 20, 2021
    Posts:
    16
    So Im using XYZ in both, however, when I create a line in 3D, Y is the direction of the height and Y and Z are the horizontal directions (on the ground).

    In my 2d project, I look from the top down to the plane to draw the lines, so the X and Y are the "grounddirections" and Z obiously doesnt exist because its 2D.

    So whenever I implement the JSON now, the lines are in the wrong place because of these differences. I understand both modes are XYZ, but they are treated differently. Should I flip the plane in the 3d project so that it fits or is there a better solution?
     
  6. M4dR0b

    M4dR0b

    Joined:
    Feb 1, 2019
    Posts:
    107
    @unity_9C31F72053387C314EDE could you share some pics, it could help.

    If you have to flip something, it would probably be better to rotate the camera in the 2D project. Or, if you don't want to flip anything, when you import the JSON you could specify which axes go where.

    Eg: You can export an array of XYZ [1,2,5], but on deserializing the json you can do [line.X = json.Z, line.Y = Json.X, line.Z = json.Y] so the new order would be XYZ [5,1,2] or whichever order works for you.
     
  7. unity_9C31F72053387C314EDE

    unity_9C31F72053387C314EDE

    Joined:
    May 20, 2021
    Posts:
    16
    I decided to go with the method where I change the desirializing, but it didnt work.

    Here is the 3D project, where the X and the Z coordinate determine the object's position on the ground https://i.ibb.co/tQmPyq2/1.png

    Here is the 2D project, where X and Y determine the object's position on the plane: https://i.ibb.co/L8kG5MY/2.png

    Here is my createline function in the 3D project (this is using the deserialized json): https://i.ibb.co/8sWYMYN/3.png
     
  8. unity_9C31F72053387C314EDE

    unity_9C31F72053387C314EDE

    Joined:
    May 20, 2021
    Posts:
    16
    I made it work. The problem layed in my loops, which erased an out of bounds exception

    The transformation of the planes worked.

    Thanks for your help!!
     
unityunity