Search code examples
c#unity-game-enginelinedrawrenderer

How change cave position of this line renderer


I am using the following code to draw a curved dotted line using a line renderer(Copied from the net). It's working but the curve is forming downward like a boat. I want to draw the curve in the opposite direction. I tried adjusting the values but no luck. can anyone can please say how to do this with this code? or can suggest me new set of code

NB- I only have very basic knowledge in programming.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLineRenderer : MonoBehaviour
{
    public Transform Point1;
    public Transform Point2;
    public Transform Point3;
    public LineRenderer linerenderer;
    public float vertexCount = 12;
    public float Point2Ypositio = 2;
    // Start is called before the first frame update
    void Start()
    {
        linerenderer.SetWidth(10, 10);
    }

   
    public void buttonPress()
    {
         Point2.transform.position = new Vector3((Point1.transform.position.x + Point3.transform.position.x)/2, Point2Ypositio, (Point1.transform.position.z + Point3.transform.position.z) *2);
        var pointList = new List<Vector3>();

        for(float ratio = 0;ratio<=1;ratio+= 1/vertexCount)
        {
            var tangent1 = Vector3.Lerp(Point1.position, Point2.position, ratio);
            var tangent2 = Vector3.Lerp(Point2.position, Point3.position, ratio);
            var curve = Vector3.Lerp(tangent1, tangent2, ratio);

            pointList.Add(curve);
        }

        linerenderer.positionCount = pointList.Count;
        linerenderer.SetPositions(pointList.ToArray());
    }
    
}

Solution

  • Set Point2Ypositio =600

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class DrawLineRenderer : MonoBehaviour
    {
        public Transform Point1;
        public Transform Point2;
        public Transform Point3;
        public LineRenderer linerenderer;
        public float vertexCount = 12;
        public float Point2Ypositio = 600;
        // Start is called before the first frame update
        void Start()
        {
            linerenderer.SetWidth(10, 10);
        }
    
       
        public void buttonPress()
        {
             Point2.transform.position = new Vector3((Point1.transform.position.x + Point3.transform.position.x)/2, Point2Ypositio, (Point1.transform.position.z + Point3.transform.position.z) *2);
            var pointList = new List<Vector3>();
    
            for(float ratio = 0;ratio<=1;ratio+= 1/vertexCount)
            {
                var tangent1 = Vector3.Lerp(Point1.position, Point2.position, ratio);
                var tangent2 = Vector3.Lerp(Point2.position, Point3.position, ratio);
                var curve = Vector3.Lerp(tangent1, tangent2, ratio);
    
                pointList.Add(curve);
            }
    
            linerenderer.positionCount = pointList.Count;
            linerenderer.SetPositions(pointList.ToArray());
        }
        
    }