Kit*_*adr 6 geometry unity-game-engine
我有一条带有线渲染器的线。用户可以移动线并旋转它。如何获取已移动或旋转的线条渲染器的新位置?由于线渲染器的顶点坐标没有变化,只有线对象整体的位置和旋转发生了变化。
图像底部的位置在移动或旋转时不会改变。这些位置由 getpositions() 方法返回,这在我的情况下没有用。
unity中的LineRenderer接受一个点列表(存储为Vector3 s)并通过它们绘制一条线。它以两种方式之一执行此操作。
局部空间:(默认)所有点都相对于变换定位。因此,如果您的游戏对象移动或旋转,线条也会移动和旋转。
世界空间:(您需要选中使用世界空间复选框)该线将在世界中与列表中的位置完全匹配的固定位置呈现。如果游戏对象移动或旋转,线条将保持不变
所以你真正想知道的是 “我如何获得我的线中局部空间点的世界空间位置?”
这个常见用例由游戏对象转换上的方法解决
它需要一个本地空间点(默认情况下,数据在线条渲染器中的存储方式)并将其转换为世界空间。
一个例子:
using UnityEngine;
using System.Collections;
public class LineRendererToWorldSpace : MonoBehaviour
{
private LineRenderer lr;
void Start()
{
lr = GetComponent<LineRenderer>();
// Set some positions in the line renderer which are interpreted as local space
// These are what you would see in the inspector in Unity's UI
Vector3[] positions = new Vector3[3];
positions[0] = new Vector3(-2.0f, -2.0f, 0.0f);
positions[1] = new Vector3(0.0f, 2.0f, 0.0f);
positions[2] = new Vector3(2.0f, -2.0f, 0.0f);
lr.positionCount = positions.Length;
lr.SetPositions(positions);
}
Vector3[] GetLinePointsInWorldSpace()
{
Vector3[] positions;
//Get the positions which are shown in the inspector
var numberOfPositions = lr.GetPositions(positions);
//Iterate through all points, and transform them to world space
for(var i = 0; i < numberOfPositions; i += 1)
{
positions[i] = transform.TransformPoint(positions[i]);
}
//the points returned are in world space
return positions;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码仅用于演示目的,因为我不确定用例。此外,我的链接指向 2018.2,这是 Unity 的最新版本,但是使用的逻辑和方法应该非常相似。
| 归档时间: |
|
| 查看次数: |
2880 次 |
| 最近记录: |