C# 无法从在线渲染器inUnity上的位置组件访问值
在这里,我将生成一个具有多个点的线渲染器,以从点中生成波浪,现在我需要访问该列表中的一些点,以便对其进行进一步的计算 问题是,我用于添加点的向量2列表与线渲染器组件上的“位置”参数显示的点不同,因为我还移动了附加此线渲染器组件的父组件,并且默认线渲染器组件上的位置显示了世界中的位置值空间,这是我需要访问的,而我生成的向量2列表显示了局部空间中的点,其中只有一个值似乎在改变 如何在不移动当前架构太多的情况下访问世界空间值? 我需要访问线渲染器组件上位置元素上的Z轴点,以便比较位置值,还需要获得相同点的索引C# 无法从在线渲染器inUnity上的位置组件访问值,c#,unity3d,C#,Unity3d,在这里,我将生成一个具有多个点的线渲染器,以从点中生成波浪,现在我需要访问该列表中的一些点,以便对其进行进一步的计算 问题是,我用于添加点的向量2列表与线渲染器组件上的“位置”参数显示的点不同,因为我还移动了附加此线渲染器组件的父组件,并且默认线渲染器组件上的位置显示了世界中的位置值空间,这是我需要访问的,而我生成的向量2列表显示了局部空间中的点,其中只有一个值似乎在改变 如何在不移动当前架构太多的情况下访问世界空间值? 我需要访问线渲染器组件上位置元素上的Z轴点,以便比较位置值,还需要获得
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
使用UnityEngine.UI;
[RequiredComponent(typeof(LineRenderer))]
公共类尾部:单一行为{
公共游戏对象组件,直径球,游戏按钮,统计工具子级;
公众浮标速度;
公共浮点数间距=.01f;
公共名单点;
线条渲染器线条;
公共文本最大值、最小值、平均值、中位数;
公共点击测量其他脚本;
//用于初始化
无效开始(){
line=GetComponent();
点=新列表();
设定点();
}
//每帧调用一次更新
无效更新(){
如果(!playButton.activeSelf)
{
if(Vector3.距离(points.Last(),diameterBall.transform.position)>点间距)
{
设定点();
}
}
/*
int result1=0;
result1=BinarySearchRecursive(points.position.x,otherScript.statisticalMarkerList[0]。transform.position.x,0,points.Count);
*/
if(statisticalToolSubLevel.activeSelf)
{
对于(int i=0;i您可以使用transform.TransformVector(Vector3点)
将变换的局部空间转换为世界空间。多亏了一位朋友,我才解决了这个问题。
我所需要做的就是通过创建一个新的不同的Vector3数组并获取SetPoint函数中的位置来访问line renderer position组件中的那些值
附加代码:
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);
}
Vector3[]最终位置;
无效设定点()
{
添加(diameterBall.transform.InverseTransformPoint(diameterBall.transform.position));
line.positionCount=points.Count;//Count我想我不能这样做,因为我在设置转换时使用了InverseTransformPoint,因为我需要嵌套组件上的这些值以某种方式运行。如果我可以访问表单中的line renderer组件的位置值,它生成的列表将解决我的pro问题…看起来您正在使用InverseTransformPoint()
设置LineRenderer
InverseTransformPoint()的位置
获取一个矢量3并假设它在世界空间中。然后它将其转换为调用它的变换的局部空间。因此,LineRenderer
位置实际上位于tailComponent
的局部空间中。如果这些是您希望访问的值,则不要使用点。添加(diameterBall.transform.position);
,使用points.Add(tailComponent.transform.InverseTransformPoint(diameterBall.transform.position));
可以将线条渲染器设置为使用局部坐标,其中有一个复选框用于that@zambari我也需要这些价值观。