Search Unity

How to set max size of Line Renderer?

Discussion in 'Scripting' started by mk22, Apr 26, 2015.

  1. mk22

    mk22

    Joined:
    Feb 1, 2015
    Posts:
    51
    Hey,

    I have a script where I'm drawing the line. I get

    Code (CSharp):
    1. startPosition when touch.phase ==TouchPhase.Began
    and
    Code (CSharp):
    1. endPosition when touch.phase ==TouchPhase.Moved
    Then I'm setting Line Renderer:

    Code (CSharp):
    1. line.SetPosition (0, new Vector3 (startPosition.x, startPosition.y, -1));
    2. line.SetPosition (1, new Vector3 (endPosition.x, endPosition.y, -1));
    It works fine but I want to clamp length of this line to some value for example 5.0f.
    I tried ClampMagnitude of endPosition but it doesn't work.
    Do you know how can I get it?

    Thanks.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Code (csharp):
    1.  
    2. float dist = Vector3.Distance(startPos, endPos);
    3. if(dist > 5)
    4. return;
    5.  
    6. else{
    7. ///set positions of lines
    8. }
     
    TheDevloper, Santako and mk22 like this.
  3. mk22

    mk22

    Joined:
    Feb 1, 2015
    Posts:
    51
    Thanks, it works now.
     
  4. IvayloDev

    IvayloDev

    Joined:
    Apr 20, 2015
    Posts:
    23
    This works, but in my game the player can drag a line and when it exceeds the max allowed value it just stop and I want to make it continue moving the line and not use return because it just freezes the line.
     
  5. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Code (csharp):
    1.  
    2. Vector3 dir = endPos - startPos;
    3. float dist = Mathf.Clamp(Vector3.Distance(startPos, endPos), 0, maxDist);
    4. endPos = startPos + (dir.normalized * dist);
    5.  
     
  6. BastingQuill

    BastingQuill

    Joined:
    Jun 30, 2020
    Posts:
    2
    4 years later... You gave me the perfect answer for my game! I appreciate it!
     
  7. Gamadrila

    Gamadrila

    Joined:
    Sep 5, 2021
    Posts:
    142
    How to set the maximum size of a line render if it consists of multiple points?
    Code (CSharp):
    1. List<Vector3> buffer = new List<Vector3>();
    2. ai2.GetRemainingPath(buffer, out bool stale);
    3. line.positionCount = buffer.Count;
    4. line.SetPositions(buffer.ToArray());
     
  8. prehprehpreh

    prehprehpreh

    Joined:
    Jul 14, 2022
    Posts:
    3
    How could I get this to work with Vector2? I can't get it to work in my instance.
     
  9. prehprehpreh

    prehprehpreh

    Joined:
    Jul 14, 2022
    Posts:
    3
    How could I get this to work with Vector2? I can't get it to work in my instance.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Please don't necro-post with vague unanswerable things like "I can't get it to work."

    Start your own fresh post... it's FREE!

    When you post, remember we cannot read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    prehprehpreh likes this.
  11. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Code (CSharp):
    1. Vector2 dir = endPos - startPos;
    2. float dist = Mathf.Clamp(Vector2.Distance(startPos, endPos), 0, maxDist);
    3. endPos = startPos + (dir.normalized * dist);
     
  12. prehprehpreh

    prehprehpreh

    Joined:
    Jul 14, 2022
    Posts:
    3
    Thanks, noted moving forward!
     
  13. ABHINAWRAM

    ABHINAWRAM

    Joined:
    Apr 15, 2022
    Posts:
    2
    It doesn't work in my case

    Is there any way to make line renderer length limit ?

    line renderer endPos follow mouse position but line length goes infinite want line renderer length limited.
     
  14. ABHINAWRAM

    ABHINAWRAM

    Joined:
    Apr 15, 2022
    Posts:
    2