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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to make levels out of just lines?

Discussion in '2D' started by captnhanky, Dec 11, 2018.

  1. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    Hi!
    I would like to make a 2D classic vector like game with only lines representing the level borders.
    My question is:
    What is the easiest way to make such levels?
    Is there a way to draw such lines direcly in unity automatically with an edge collider attached?
    Is there maybe an asset for this?
    A good example for this is gravitar


    THANKS!
     
  2. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    I suggest using the line renderer component and edge colliders.
    You could make a script that executes during update so you can build the level in the editor.
    You could make the script draw a line from its game object and lead to another game object, which in turns leads to antoher and another, etc...

    It's a cool idea, I might give this a go myself one day.


    Code (CSharp):
    1. public class VertexScript : MonoBehaviour
    2. {
    3.     [SerializeField] private LineRenderer lRenderer;
    4.     [SerializeField] private Transform target;
    5.     [SerializeField] private float lineWidth = 0.2f;
    6.  
    7.     void Awake()
    8.     {
    9.         lRenderer.startWidth = lineWidth;
    10.         lRenderer.endWidth = lineWidth;
    11.     }
    12.  
    13.     [ExecuteInEditMode]
    14.     void Update()
    15.     {
    16.         lRenderer.SetPosition ( 0 , transform.position );
    17.         lRenderer.SetPosition ( 1 , target.position );
    18.     }
    19. }
    20.  
     
    Last edited: Dec 11, 2018
  3. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    Thanks for your suggestion!
    I tried it out and it works for drawing lines. Very cool.

    BUT:

    I could not manage the edge collider to work with the drawn lines.
     
  4. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    okey dokey, give this a whirl.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent ( typeof ( LineRenderer ) )]
    4. [RequireComponent ( typeof ( EdgeCollider2D ) )]
    5. public class VertexScript : MonoBehaviour
    6. {
    7.     private LineRenderer lRenderer;
    8.     private EdgeCollider2D edgeCollider2D;
    9.  
    10.     [SerializeField] private Transform target;
    11.     [SerializeField] private Color lineColor = Color.green;
    12.  
    13.     private float lineWidth = 0.05f;
    14.  
    15.     [ExecuteInEditMode]
    16.     private void Update()
    17.     {
    18.         // Move me to Start() or Awake()
    19.         {
    20.                 lRenderer = GetComponent<LineRenderer> ( );
    21.                 edgeCollider2D = GetComponent<EdgeCollider2D> ( );
    22.         }
    23.         //width
    24.         lRenderer.startWidth = lineWidth;
    25.         lRenderer.endWidth = lineWidth;
    26.         //colour
    27.         lRenderer.startColor = lineColor;
    28.         lRenderer.endColor = lineColor;
    29.         //position
    30.         lRenderer.SetPosition ( 0 , transform.position );
    31.         lRenderer.SetPosition ( 1 , target.position );
    32.  
    33.         Vector2 [ ] points = new Vector2 [ 2 ]
    34.         {
    35.             Vector2.zero,
    36.             target.position - transform.position
    37.         };
    38.  
    39.         //update the edge colliders points
    40.         edgeCollider2D.points = points;
    41.     }
    42. }
     
    Last edited: Dec 11, 2018
    APSchmidt likes this.
  5. cryogee

    cryogee

    Joined:
    Aug 6, 2009
    Posts:
    132
    There is also a Vectrosity package which is good for Lines stuff.

    In case you decide to move from straight to curved/bezier/circles lines, it will have ready methods to create those
     
  6. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    Does the vectrosity package offer a collision dedection for lines? Is it possible to make a game like gravitar with it?
     
  7. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    Thimble2600


    Big thanks for your support!

    Unfortunately it does not work for me. I played around with it for kind a while now.
    What makes me suspicious is the fact that in your screenshot there are 3 instances of vertex.
    The target of vertex is vertex(1).
    I tried this configuration besides of many others but no luck.
     
  8. cryogee

    cryogee

    Joined:
    Aug 6, 2009
    Posts:
    132
    Yes. You just have to set the collider property to true and it will generate colliders for your line.

    Yes you should be able to create Gravitar like game with it.

    Code (CSharp):
    1. myLine.collider = true;
     
  9. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    Thank you, I think I will give it a try (although it is not free ;) )
     
  10. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97

    Ok I purchased the vectrosity package but no luck with the collider in scene mode.
    I turned on the "Collider" in the Vector Object 2D (script) as described in the manual to activate the collider on the lines but it does not work. I am using the 2D Space in unity as also described in the manual.

    Using a script to draw points makes no sense to me because I want to see what I am drawing.
    I am a little bit disapointed to say the least.

    Any suggestions?
    Thanks
     
  11. cryogee

    cryogee

    Joined:
    Aug 6, 2009
    Posts:
    132
    What do you mean by that? I added a line. Enabled Collider and Saw the collider was applied to it.
    Click on Edit collider to see the collider in scene space .
    What else are expecting, Please clarify.

     
  12. captnhanky

    captnhanky

    Joined:
    Dec 31, 2015
    Posts:
    97
    After some tries I got it to work.
    I don´t know why , but when I added a new scene it suddenly worked.
    Before I could not manage the edge collider to add or update points.

    What I am really missing badly is the ability to add points between two existing points in the visual editor. I can only remove points but not add them.
    I contacted the author but he said that this would be a feature of a quote "super-complex bloated drawing app" and he also claims that the visual editor is not meant to be used at all.
    Funny statement, to say at least.



    Anyway thank you
     
    Last edited: Dec 13, 2018