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

How can I use one line render component to draw two different types of lines

Discussion in '2D' started by Lil_Potatoe, Apr 18, 2021.

  1. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Hey, so I'm working on a Drag and Shoot game which means my player has a line render to draw an arrow for their input. I also added a grappling hook-like mechanism to the player that also utilizes a line renderer component, but the problem is it uses the settings for the arrow. Is there a way I can get around this or do I need to create a separate game object for my grappling mechanism to make it draw its own type of line?
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    If both use the same line renderer, both will end up interfering with each other
    If you want to draw 2 different lines that are not related to each other, you want to use 2 different line renderers

    So yea, 2 different game objects would be the easiest solution here

    tho you dont need to move your entire grapling mechanism over to the other game object, instead you can still use your current script and just link the new renderer to it:
    just make a game object that represents the grappling Hook graphics (depending on what you need this can be done as a child of the player),
    give it a line renderer,
    add a LineRenderer variable to your current script (like [SerializeField] private LineRenderer hookRenderer),
    drag your new line renderer game object into the new line renderer field in the inspector of your player,
    and then just replace the usage of your other line renderer with the new one

    I m saying this because the way you wrote your question sounds like you are trying to squeeze all your player logic into a single game object, and as if you try to only acces native components of your game object.
    You dont have to do this, usually ppl split off the entities of their games into multiple game objects to make things easier maintainable and exchangeable

    I usually do something like this: (with A > B meaning that A is the parent object of B)
    Player > Physics > Gfx > {A collection of graphic objects, depending on what I need}
    Player > NonPhysicsGfx

    Where in the highest level "Player" I only have the logic (defined mechanisms in scripts, which steer the lower level game objects), Physics is the object i acutally move around and that has a collider, Gfx has the Animator, the children of it have all the renderers

    I write this just as a reminder that you dont have to squeeze everything you do into a single GameObject^^
     
    Last edited: Apr 19, 2021
    deathfrights and Lil_Potatoe like this.
  3. Lil_Potatoe

    Lil_Potatoe

    Joined:
    Feb 2, 2021
    Posts:
    16
    Thanks! I'm a beginner at this stuff so once again thank you.