我试图将用线渲染器绘制的线的末端与空间中的一点相匹配,该点距相机只有几个单位,并且位于可能离相机很远或很近的对象上方。当我使用WorldToScreenPoint时,得到的结果令我难以理解。将位置设置为0是我无法理解的结果。该代码是我试图获取其屏幕位置的对象的组成部分。任何帮助表示赞赏。
foreach (int i in identifiers)
{
GameObject lineHolder = new GameObject();
lineHolder.transform.position = transform.position;
lineHolder.transform.parent = transform;
LineRenderer line = lineHolder.AddComponent<LineRenderer>();
lineHolders.Add(lineHolder);
line.positionCount = 2;
line.SetPosition(0, Camera.main.WorldToScreenPoint(new Vector3(transform.position.x, transform.position.y, master.screenPoint.z)));
line.widthMultiplier = .1f;
line.SetPosition(1, master.controlPanel.controlElements[i].position);
}
这就是我在我所指的“主”代码中获得“ screenPoint”的方式:
screenPoint = Camera.main.WorldToScreenPoint(controlPanel.transform.position);
“ controlPanel”对象距离摄像机5个单位。我试图避免在控制面板中的对象下方绘制线条,这些对象只是对象,不是画布的一部分,而是与相机保持5个单位的距离。
我从屏幕点转换为世界点。这是有效的更正代码:
foreach (int i in identifiers)
{
GameObject lineHolder = new GameObject();
lineHolder.transform.position = transform.position;
lineHolder.transform.parent = transform;
LineRenderer line = lineHolder.AddComponent<LineRenderer>();
lineHolders.Add(lineHolder);
line.positionCount = 2;
Vector3 screenPoint = Camera.main.WorldToScreenPoint(transform.position);
line.SetPosition(0, Camera.main.ScreenToWorldPoint(new Vector3(screenPoint.x, screenPoint.y, master.controlPanel.transform.localPosition.z)));
line.widthMultiplier = .1f;
line.SetPosition(1, master.controlPanel.controlElements[i].position);