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. Dismiss Notice

Drawing Connection In Editor

Discussion in 'Scripting' started by GrayWoolsey, Jul 29, 2020.

  1. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    So think of a scene with portals and each portal has a connected portal so when you enter it, you always go to the other one and vise-versa. Now my question is, is there a way to show this in the Editor. When I'm creating my scenes can I have a visual representation in the editor of which portal would be connected to which via a line connection or something?
     
  2. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Maybe create a protal with a list then keep track in some other script how many other protals have been spawned and keep adding the portals that now get spawned (assuming there can be more than 2 but only two connected) and if said number (in my exsample 2) had been reached set it back to zero and say the first portal to get the direction to the second and drawing a ray in that direction with the distance


    The first Portal
    Code (CSharp):
    1.  
    2. public List<Transform> Portals;
    3.  
    4. void Start()
    5. {
    6. //Add this Portal
    7. Portals.Add(this.transform);
    8. }
    9.  
    10. void Update()
    11. {
    12. //Checking if enough Portals exist to not freak the computer out
    13. if(Portals.Count <= 2)
    14. {
    15.   Vector 3 dir = transform.positon - Portals[1].position;
    16.   float dist = Vector3.Distance(transform.position, Portals[1].position);
    17.  
    18.   //Draw Ray from Position in direction with distance
    19.   Debug.DrawRay(transform.position, dir * dist);
    20. }
    21. }
    The "PotalHandler"
    Code (CSharp):
    1.  
    2. public int totalPortalsSpanwned = 0;
    3. pu
    4. public Portal firstPotal;
    5.  
    6.  
    7. //Adress how many Portals should be made at maximum and add first Portal
    8. void AddPotal(int maxNumberOfPortals, Portal portal)
    9. {
    10. if(totalPortalsSpanwned != maxNumberOfPortals)
    11. {
    12.   if(ttotalPortalsSpanwned == 0)
    13.    {
    14.     firstPortal = portal;
    15.    }
    16.  
    17.   totalPortalsSpanwned++;
    18. }else{
    19.  firstPortal.Portals.Add(portal);
    20.  
    21.  //Reset to normal to make next two portals
    22.  firstPortal = null;
    23.  totalPortalsSpanwned = 0;
    24.  
    25. }
    26. }
     
    Last edited: Jul 29, 2020
    GrayWoolsey likes this.
  3. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    So this is good and it will work but how can I get this to run when the game isn't. When I'm building out my level I wanted to see the rays while the game isn't running.
     
  4. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Ah so the player can't create portals they are already there? Because then all you need to do is place the portals and assign the second portal to the List of the first one and add [ExecuteInEditMode] at the top of the portal script
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
  6. GrayWoolsey

    GrayWoolsey

    Joined:
    Apr 20, 2018
    Posts:
    27
    Yes! Thank you very much. I'm just using portals as an example. I'm prototyping a roguelike with this 3d world but to make the game less flat there are stairs and inclines but for now the player is just going to teleport up stairs because for the terms of the prototype I am far too lazy to have it look good and work for now and I just wanted to see the connections so I don't get messed up while scene building.
     
  7. TheOtherUserName

    TheOtherUserName

    Joined:
    May 30, 2020
    Posts:
    136
    Otherwise you can use void OnDrawGizmosSelected I think too just put it in there if [ExecuteInEditMode] freaks you computer to much out