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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Line Renderer as a child to GameObject

Discussion in 'Getting Started' started by ImpromptuTriplet, Jul 18, 2018.

  1. ImpromptuTriplet

    ImpromptuTriplet

    Joined:
    Jul 18, 2018
    Posts:
    7
    I'm attempting to create the effect of a fishing rod in Unity 2D. I'm currently using a sprite to represent the pole, an empty game object as a child to represent the starting point of the fishing line, and another empty object as a child containing a line renderer to create the fishing line. Because the line renderer uses world space, this makes moving the fishing pole in the editor rather awkward. Would a better way to structure this relationship between the fishing rod and the line be to have 2 separate objects to allow manipulation of the fishing rod in the editor to be easier? I would think doing so would make the scripting a little more complicated than it currently is.

    The project files are linked below so you can visually see what I mean if needed:

    http://www.mediafire.com/file/nrfnj2j367caqe9/FishingProject.zip
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    JoeStrout likes this.
  3. ImpromptuTriplet

    ImpromptuTriplet

    Joined:
    Jul 18, 2018
    Posts:
    7
    Yes, I'm currently using SetPosition in the script of the Fishing Rod object.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LineController : MonoBehaviour {
    6.  
    7.     public GameObject lineStart;
    8.     public Vector3 lineStartPos;
    9.  
    10.     public GameObject line;
    11.     public LineRenderer lineRenderer;
    12.     public Vector3 lineEndPos;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         // Get the position of the lineStart GameObject
    17.         lineStartPos = lineStart.transform.position;
    18.  
    19.         // Get the line renderer component
    20.         lineRenderer = line.GetComponent<LineRenderer>();
    21.         lineRenderer.positionCount = 2;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.         // Update the position of the lineStartPos
    27.         lineStartPos = lineStart.transform.position;
    28.  
    29.         // Update the position of the lineEndPos
    30.         // TODO: Currently just subtracts 5 from the starting position
    31.         lineEndPos = lineStartPos + new Vector3(0f, -5f, 0f);
    32.  
    33.         // Update the point positions of the line renderer
    34.         lineRenderer.SetPosition(0, lineStartPos);
    35.         lineRenderer.SetPosition(1, lineEndPos);
    36.     }
    37. }
    38.  
    I'm talking about how moving the object in the scene editor is strange, here's a video so you can see what I mean:



    In case you're wondering the FishingRodPivot object is to make the fishing rod pivot from the end where it would be held from instead of from the center.
     
  4. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    I looked at your video. I think you are confused about the drag functionality in the editor. You use the arrow widgets to move in any of the 3 axis and the box to drag using either both X and Y or X, Y and Z. 'Use World Space' will determine if it is a 2D or 3D drag. When 'Use World Space' is ticked you are dragging on the Z axis also.
     
  5. ImpromptuTriplet

    ImpromptuTriplet

    Joined:
    Jul 18, 2018
    Posts:
    7
    The Z value doesn't seem to change when I drag using the box. I guess I'm more confused as to why the box doesn't appear in the center of the sprite as normal. If I had to guess I would assume it's because the line renderer object is placed in the center of the world when the "Use Worldspace" option is ticked so the parent object tries to place the box in the center position relative to both the center of the sprite and the center of the world. I guess my question is if there's a good way to avoid this because it makes positioning the object harder.