Pratham Sehgal
Pratham Sehgal

Reputation: 153

Unable to access values from positions component on Line Renderer inUnity

Here I'm generating a line renderer with multiple points to produce a wave out of the points and now I need to access some points in that list to do further calculations on it.

enter image description here

The problem is the Vector 2 List that I'm using to add the points to does not show the same points as that of Positions parameter on the line renderer component as I'm also moving the parent component on which this line renderer component is attached and the position on the default line renderer component shows the positional values in World Space which is what I need to access, whereas the Vector 2 list that I've generated shows points in Local Space, where only one value appears to be changing.

enter image description here

How can I access the World space values without moving too much of my current architecture? I need to access Z axis points on Positions elements on Line Renderer component for comparing the positional values and also get Index of the same points as well.

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(LineRenderer))]
public class tail : MonoBehaviour {

    public GameObject tailComponent, diameterBall, playButton, statisticalToolSubLevel;
    public float speed;

    public float pointSpacing = .01f;

    public List<Vector2> points;

    LineRenderer line;

    public Text maxima, minima, mean, median;

    public TapToMeasure otherScript;

    // Use this for initialization
    void Start () {
        line = GetComponent<LineRenderer>();

        points = new List<Vector2>();

        SetPoint();

    }

    // Update is called once per frame
    void Update () {

        if (!playButton.activeSelf)
        {
            if (Vector3.Distance(points.Last(), diameterBall.transform.position) > pointSpacing)
            {
                SetPoint();
            }
        }
        /*
        int result1 = 0;
        result1 = BinarySearchRecursive(points.position.x, otherScript.statisticalMarkerList[0].transform.position.x, 0, points.Count);
        */

        if (statisticalToolSubLevel.activeSelf)
        {
            for(int i=0; i<points.Count; i++)
            { 
                if(Mathf.Approximately(points[i].x, otherScript.statisticalMarkerList[0].transform.position.x))
                {
                    Debug.Log("Task acheieved");
                    //return i;
                }

                if (points[i].x < otherScript.statisticalMarkerList[0].transform.position.x)
                {
                    i += 1;
                    //Debug.Log(i);
                }
            }   
        }
    }

    void SetPoint()
    {
        points.Add(diameterBall.transform.position);

        line.positionCount = points.Count; //count<=100 or whatever number or edge of the room
        //line.SetPosition(points.Count - 1, diameterBall.transform.position);
        line.SetPosition(points.Count - 1, tailComponent.transform.InverseTransformPoint(diameterBall.transform.position));

    }

    public static int BinarySearchRecursive(float[] inputArray, float query, int min_idx, int max_idx)
    {
        if (min_idx > max_idx)
        {
            return -1;
        }
        else
        {
            int mid = (min_idx + max_idx) / 2;
            if (query == inputArray[mid])
            {
                return mid;
            }
            else if (query < inputArray[mid])
            {
                return BinarySearchRecursive(inputArray, query, min_idx, mid);
            }
            else if (min_idx + 1 == max_idx)
            {
                return mid;
            }
            else
            {
                return BinarySearchRecursive(inputArray, query, mid, max_idx);
            }
        }
    }
}

Upvotes: 0

Views: 343

Answers (2)

Pratham Sehgal
Pratham Sehgal

Reputation: 153

Thanks to a friend I was able to solve this problem. All I was needed to do was access those values in the line renderer position component by creating a new different Vector3 array and getting the positions in the SetPoint function.

Additional Code:

Vector3[] finalpositions;

void SetPoint()
    {
        points.Add(diameterBall.transform.InverseTransformPoint(diameterBall.transform.position));

        line.positionCount = points.Count; //count<=100 or whatever number or edge of the room
        //line.SetPosition(points.Count - 1, diameterBall.transform.position);
        line.SetPosition(points.Count - 1, tailComponent.transform.InverseTransformPoint(diameterBall.transform.position));
        finalpositions = new Vector3[points.Count];
        line.GetPositions(finalpositions);
    }

Upvotes: 0

Adam Stox
Adam Stox

Reputation: 457

You can use transform.TransformVector(Vector3 point) to convert from the local space of your transform to worldspace.

Upvotes: 1

Related Questions