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

Bezier curve linerenderer doesn't work ....

Discussion in 'Scripting' started by Bobindiana66, Aug 4, 2021.

  1. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Hi!
    I have a problem with linerenderer and position
    I change the first part with lengthOfLineRenderer to avoid the first error.
    I dont'succed to change the second part of line renderer,
    lineRenderer Ir = SetPositions(position); don't work .......

    in the API unity
    Code (csharp):
    1.  
    2. public int lengthOfLineRenderer = 20;
    3.  
    4.     void Start()
    5.    {
    6.        [URL='https://docs.unity3d.com/ScriptReference/LineRenderer.html']LineRenderer[/URL] lineRenderer = gameObject.AddComponent<[URL='https://docs.unity3d.com/ScriptReference/LineRenderer.html']LineRenderer[/URL]>();
    7.        lineRenderer.material = new [URL='https://docs.unity3d.com/ScriptReference/Material.html']Material[/URL]([URL='https://docs.unity3d.com/ScriptReference/Shader.Find.html']Shader.Find[/URL]("Sprites/Default"));
    8.        lineRenderer.widthMultiplier = 0.2f;
    9.        lineRenderer.positionCount = lengthOfLineRenderer;
    10.  
    11.  
    12.  
    13. The script ..............
    14. Thanks in adavance.
    15. [code]
    16. using System.Collections;
    17. using System.Collections.Generic;
    18. using UnityEngine;
    19.  
    20. public class Bezier : MonoBehaviour
    21. {
    22.    public LineRenderer LineRenderer;
    23.    public Transform point0, point1, point2, point3;
    24.    private int numPoints = 50;
    25.    private Vector3[] position = new Vector3[50];
    26.    private LineRenderer lr;
    27.     void Start(){
    28.     LineRenderer lr = GetComponent<LineRenderer>();
    29.     lr.positionCount = numPoints;
    30.     DrawCubicCurve();
    31.  
    32.      
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.        DrawCubicCurve();
    39.     }
    40.  
    41.     private void DrawCubicCurve(){
    42.         for (int i = 1; i < numPoints +1; i++){
    43.             float t = i / (float)numPoints;
    44.             position[i - 1] = CalculateCubicBezierPoint(t, point0.position, point1.position, point2.position, point3.position);
    45.         }
    46.         lineRenderer.SetPositions(position);
    47.     }
    48.  
    49.     private Vector3 CalculateCubicBezierPoint(float t,Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3){
    50.     float u = 1- t;
    51.     float tt = t * t;
    52.     float uu = u * u;
    53.     float uuu= uu * u;
    54.     float ttt = tt * t;
    55.     Vector3 p = uuu * p0;
    56.     p += 3 * uu * t * p1;
    57.     p += 3 * u * tt * p2;
    58.     p += ttt * p3;
    59.     return p;
    60.     }
    61. }
     
    Last edited: Aug 5, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    A problem you say!! Wow.

    It's always good to avoid ALL errors.

    Hopefully one day soon you'll "succed."

    Meanwhile, here are some hints that might help you actually get useful information when you post in a public forum where people cannot read your mind from afar.

    How to report your problem productively in the Unity3D forums:

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

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    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/

    Use code tags:
     
    Yoreki likes this.
  3. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    I have follow the rules, sorry ..........
    the last error, i change a little but the renderer not working.
    I'm using visual studio code.
    The package renderer is in the unity or it is an addon ?
    Thanks.


    Assets\NoSpline.cs(32,19): error CS0103: The name 'SetPositions' does not exist in the current context
    Assets\NoSpline.cs(32,1): error CS0246: The type or namespace name 'lineRenderer' could not be found (are you missing a using directive or an assembly reference?)

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NoSpline : MonoBehaviour
    6. {
    7. public LineRenderer LineRenderer;
    8. public Transform point0, point1, point2, point3;
    9. private int numPoints = 50;
    10. private Vector3[] position = new Vector3[50];
    11. private LineRenderer lr;
    12. void Start(){
    13. LineRenderer lr = GetComponent<LineRenderer>();
    14. lr.positionCount = numPoints;
    15. DrawCubicCurve();
    16.  
    17.  
    18. }
    19.  
    20. // Update is called once per frame
    21. void Update()
    22. {
    23. DrawCubicCurve();
    24. }
    25.  
    26. private void DrawCubicCurve(){
    27. for (int i = 1; i < numPoints +1; i++){
    28. float t = i / (float)numPoints;
    29. position[i - 1] = CalculateCubicBezierPoint(t, point0.position, point1.position, point2.position, point3.position);
    30. }
    31. lineRenderer lr = SetPositions(position);
    32. }
    33.  
    34. private Vector3 CalculateCubicBezierPoint(float t,Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3){
    35. float u = 1- t;
    36. float tt = t * t;
    37. float uu = u * u;
    38. float uuu= uu * u;
    39. float ttt = tt * t;
    40. Vector3 p = uuu * p0;
    41. p += 3 * uu * t * p1;
    42. p += 3 * u * tt * p2;
    43. p += ttt * p3;
    44. return p;
    45. }
    46. }
     
  4. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    And at last this script is good but it is using a lot of scripts............
    whitout Renderer ..........
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public static class Bezier {
    5.  
    6.     public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, float t) {
    7.         t = Mathf.Clamp01(t);
    8.         float oneMinusT = 1f - t;
    9.         return
    10.             oneMinusT * oneMinusT * p0 +
    11.             2f * oneMinusT * t * p1 +
    12.             t * t * p2;
    13.     }
    14.  
    15.     public static Vector3 GetFirstDerivative (Vector3 p0, Vector3 p1, Vector3 p2, float t) {
    16.         return
    17.             2f * (1f - t) * (p1 - p0) +
    18.             2f * t * (p2 - p1);
    19.     }
    20.  
    21.     public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
    22.         t = Mathf.Clamp01(t);
    23.         float OneMinusT = 1f - t;
    24.         return
    25.             OneMinusT * OneMinusT * OneMinusT * p0 +
    26.             3f * OneMinusT * OneMinusT * t * p1 +
    27.             3f * OneMinusT * t * t * p2 +
    28.             t * t * t * p3;
    29.     }
    30.  
    31.     public static Vector3 GetFirstDerivative (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
    32.         t = Mathf.Clamp01(t);
    33.         float oneMinusT = 1f - t;
    34.         return
    35.             3f * oneMinusT * oneMinusT * (p1 - p0) +
    36.             6f * oneMinusT * t * (p2 - p1) +
    37.             3f * t * t * (p3 - p2);
    38.     }
    39. }
    40.  
    41. [code]
    42. using UnityEngine;
    43.  
    44. public class BezierCurve : MonoBehaviour {
    45.  
    46.     public Vector3[] points;
    47.    
    48.     public Vector3 GetPoint (float t) {
    49.         return transform.TransformPoint(Bezier.GetPoint(points[0], points[1], points[2], points[3], t));
    50.     }
    51.    
    52.     public Vector3 GetVelocity (float t) {
    53.         return transform.TransformPoint(Bezier.GetFirstDerivative(points[0], points[1], points[2], points[3], t)) - transform.position;
    54.     }
    55.    
    56.     public Vector3 GetDirection (float t) {
    57.         return GetVelocity(t).normalized;
    58.     }
    59.    
    60.     public void Reset () {
    61.         points = new Vector3[] {
    62.             new Vector3(1f, 0f, 0f),
    63.             new Vector3(2f, 0f, 0f),
    64.             new Vector3(3f, 0f, 0f),
    65.             new Vector3(4f, 0f, 0f)
    66.         };
    67.     }
    68. }
    69.  
    70. [code]
    71. using UnityEngine;
    72. using System;
    73.  
    74. public class BezierSpline : MonoBehaviour {
    75.  
    76.     [SerializeField]
    77.     private Vector3[] points;
    78.  
    79.     [SerializeField]
    80.     private BezierControlPointMode[] modes;
    81.  
    82.     [SerializeField]
    83.     private bool loop;
    84.  
    85.     public bool Loop {
    86.         get {
    87.             return loop;
    88.         }
    89.         set {
    90.             loop = value;
    91.             if (value == true) {
    92.                 modes[modes.Length - 1] = modes[0];
    93.                 SetControlPoint(0, points[0]);
    94.             }
    95.         }
    96.     }
    97.  
    98.     public int ControlPointCount {
    99.         get {
    100.             return points.Length;
    101.         }
    102.     }
    103.  
    104.     public Vector3 GetControlPoint (int index) {
    105.         return points[index];
    106.     }
    107.  
    108.     public void SetControlPoint (int index, Vector3 point) {
    109.         if (index % 3 == 0) {
    110.             Vector3 delta = point - points[index];
    111.             if (loop) {
    112.                 if (index == 0) {
    113.                     points[1] += delta;
    114.                     points[points.Length - 2] += delta;
    115.                     points[points.Length - 1] = point;
    116.                 }
    117.                 else if (index == points.Length - 1) {
    118.                     points[0] = point;
    119.                     points[1] += delta;
    120.                     points[index - 1] += delta;
    121.                 }
    122.                 else {
    123.                     points[index - 1] += delta;
    124.                     points[index + 1] += delta;
    125.                 }
    126.             }
    127.             else {
    128.                 if (index > 0) {
    129.                     points[index - 1] += delta;
    130.                 }
    131.                 if (index + 1 < points.Length) {
    132.                     points[index + 1] += delta;
    133.                 }
    134.             }
    135.         }
    136.         points[index] = point;
    137.         EnforceMode(index);
    138.     }
    139.  
    140.     public BezierControlPointMode GetControlPointMode (int index) {
    141.         return modes[(index + 1) / 3];
    142.     }
    143.  
    144.     public void SetControlPointMode (int index, BezierControlPointMode mode) {
    145.         int modeIndex = (index + 1) / 3;
    146.         modes[modeIndex] = mode;
    147.         if (loop) {
    148.             if (modeIndex == 0) {
    149.                 modes[modes.Length - 1] = mode;
    150.             }
    151.             else if (modeIndex == modes.Length - 1) {
    152.                 modes[0] = mode;
    153.             }
    154.         }
    155.         EnforceMode(index);
    156.     }
    157.  
    158.     private void EnforceMode (int index) {
    159.         int modeIndex = (index + 1) / 3;
    160.         BezierControlPointMode mode = modes[modeIndex];
    161.         if (mode == BezierControlPointMode.Free || !loop && (modeIndex == 0 || modeIndex == modes.Length - 1)) {
    162.             return;
    163.         }
    164.  
    165.         int middleIndex = modeIndex * 3;
    166.         int fixedIndex, enforcedIndex;
    167.         if (index <= middleIndex) {
    168.             fixedIndex = middleIndex - 1;
    169.             if (fixedIndex < 0) {
    170.                 fixedIndex = points.Length - 2;
    171.             }
    172.             enforcedIndex = middleIndex + 1;
    173.             if (enforcedIndex >= points.Length) {
    174.                 enforcedIndex = 1;
    175.             }
    176.         }
    177.         else {
    178.             fixedIndex = middleIndex + 1;
    179.             if (fixedIndex >= points.Length) {
    180.                 fixedIndex = 1;
    181.             }
    182.             enforcedIndex = middleIndex - 1;
    183.             if (enforcedIndex < 0) {
    184.                 enforcedIndex = points.Length - 2;
    185.             }
    186.         }
    187.  
    188.         Vector3 middle = points[middleIndex];
    189.         Vector3 enforcedTangent = middle - points[fixedIndex];
    190.         if (mode == BezierControlPointMode.Aligned) {
    191.             enforcedTangent = enforcedTangent.normalized * Vector3.Distance(middle, points[enforcedIndex]);
    192.         }
    193.         points[enforcedIndex] = middle + enforcedTangent;
    194.     }
    195.  
    196.     public int CurveCount {
    197.         get {
    198.             return (points.Length - 1) / 3;
    199.         }
    200.     }
    201.  
    202.     public Vector3 GetPoint (float t) {
    203.         int i;
    204.         if (t >= 1f) {
    205.             t = 1f;
    206.             i = points.Length - 4;
    207.         }
    208.         else {
    209.             t = Mathf.Clamp01(t) * CurveCount;
    210.             i = (int)t;
    211.             t -= i;
    212.             i *= 3;
    213.         }
    214.         return transform.TransformPoint(Bezier.GetPoint(points[i], points[i + 1], points[i + 2], points[i + 3], t));
    215.     }
    216.    
    217.     public Vector3 GetVelocity (float t) {
    218.         int i;
    219.         if (t >= 1f) {
    220.             t = 1f;
    221.             i = points.Length - 4;
    222.         }
    223.         else {
    224.             t = Mathf.Clamp01(t) * CurveCount;
    225.             i = (int)t;
    226.             t -= i;
    227.             i *= 3;
    228.         }
    229.         return transform.TransformPoint(Bezier.GetFirstDerivative(points[i], points[i + 1], points[i + 2], points[i + 3], t)) - transform.position;
    230.     }
    231.    
    232.     public Vector3 GetDirection (float t) {
    233.         return GetVelocity(t).normalized;
    234.     }
    235.  
    236.     public void AddCurve () {
    237.         Vector3 point = points[points.Length - 1];
    238.         Array.Resize(ref points, points.Length + 3);
    239.         point.x += 1f;
    240.         points[points.Length - 3] = point;
    241.         point.x += 1f;
    242.         points[points.Length - 2] = point;
    243.         point.x += 1f;
    244.         points[points.Length - 1] = point;
    245.  
    246.         Array.Resize(ref modes, modes.Length + 1);
    247.         modes[modes.Length - 1] = modes[modes.Length - 2];
    248.         EnforceMode(points.Length - 4);
    249.  
    250.         if (loop) {
    251.             points[points.Length - 1] = points[0];
    252.             modes[modes.Length - 1] = modes[0];
    253.             EnforceMode(0);
    254.         }
    255.     }
    256.    
    257.     public void Reset () {
    258.         points = new Vector3[] {
    259.             new Vector3(1f, 0f, 0f),
    260.             new Vector3(2f, 0f, 0f),
    261.             new Vector3(3f, 0f, 0f),
    262.             new Vector3(4f, 0f, 0f)
    263.         };
    264.         modes = new BezierControlPointMode[] {
    265.             BezierControlPointMode.Free,
    266.             BezierControlPointMode.Free
    267.         };
    268.     }
    269. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You have at least THREE separate LineRenderer variables, all very confusingly named:

    Code (csharp):
    1. public LineRenderer LineRenderer;
    2. private LineRenderer lr;
    3.  
    4. // and inside of your Start() method you have created
    5. // yet another fresh local variable sharing the name:
    6. LineRenderer lr = ...
    Finally you are accessing something called
    lineRenderer
    that does not exist.

    If you are going to monkey-hammer-bang tutorial code in, this will save you a LOT of time:

    How to do tutorials properly:

    Tutorials are a GREAT idea. Tutorials should be used this way:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly. Fortunately this is the easiest part to get right. Be a robot. Don't make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE.

    If you get any errors, learn how to read the error code and fix it. Google is your friend here. Do NOT continue until you fix the error. The error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Imphenzia / imphenzia - super-basic Unity tutorial:



    Jason Weimann:



    Brackeys super-basic Unity Tutorial series:



    Sebastian Lague Intro to Game Development with Unity and C#:

     
    arfish likes this.
  6. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Thanks, i will made my homeworks, but at last lineRenderer is running only with hierarchy of Unity, i have a little script to
    make a ling between the 3 cubes. It is working.
    Without one definition of lineRenderer first?

    ----------------> you need to create linerenderer (with effect in component)

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Try01 : MonoBehaviour
    7. {
    8.     LineRenderer lr;
    9.     public Transform[] Positions;
    10.     void Start()
    11.     {
    12.         lr = GetComponent<LineRenderer>();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         lr.positionCount = Positions.Length;
    19.         lr.SetPosition(0, Positions[0].position);
    20.         lr.SetPosition(1, Positions[1].position);
    21.         lr.SetPosition(2, Positions[2].position);
    22.     }
    23. }
    24.  
    25.  
    26. [ATTACH=full]902018[/ATTACH]
     
    Last edited: Aug 4, 2021
  7. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
  8. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    Finally, avoid the first script with definition not simple.
    At time, this second script is running good, but at first i have to create 3 points and one line renderer to join them.

    the code
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [ExecuteInEditMode]
    7. public class BezierCurve3PointLineRenderer : MonoBehaviour
    8. {
    9.     public Transform point1;
    10.     public Transform point2;
    11.     public Transform point3;
    12.     public LineRenderer lineRenderer;
    13.     public int vertexCount = 12;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start()
    18.     {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         var pointList = new List<Vector3>();
    26.         for (float ratio = 0; ratio <= 1; ratio += 1.0f / vertexCount)
    27.         {
    28.             var tangentLineVertex1 = Vector3.Lerp(point1.position, point2.position, ratio);
    29.             var tangentLineVertex2 = Vector3.Lerp(point2.position, point3.position, ratio);
    30.             var bezierpoint = Vector3.Lerp(tangentLineVertex1, tangentLineVertex2, ratio);
    31.             pointList.Add(bezierpoint);
    32.         }
    33.         lineRenderer.positionCount = pointList.Count;
    34.         lineRenderer.SetPositions(pointList.ToArray());
    35.     }
    36.  
    37.     private void OnDrawGizmos()
    38.     {
    39.         Gizmos.color = Color.green;
    40.         Gizmos.DrawLine(point1.position, point2.position);
    41.  
    42.         Gizmos.color = Color.cyan;
    43.         Gizmos.DrawLine(point2.position, point3.position);
    44.  
    45.         Gizmos.color = Color.red;
    46.         for (float ratio = 0.5f / vertexCount;ratio<1;ratio += 1.0f / vertexCount)
    47.         {
    48.             Gizmos.DrawLine(Vector3.Lerp(point1.position, point2.position, ratio), Vector3.Lerp(point2.position, point3.position, ratio));
    49.         }
    50.     }
    51. }
     
  9. Bobindiana66

    Bobindiana66

    Joined:
    Jul 25, 2021
    Posts:
    21
    BEZIER Fin.JPG
     
    arfish likes this.