使用 linerenderer 和 raycast 在两个游戏对象之间画线
Draw line between two game objects using linerenderer and raycast
我想做的是在我使用光线距离和线渲染器的两个游戏对象之间画线,但线太长了。下面是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineTest : MonoBehaviour
{
RaycastHit hit;
public LineRenderer lineRender;
private void Awake()
{
lineRender = GetComponent<LineRenderer>();
}
void Start()
{
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
lineRender.enabled = true;
lineRender.SetPosition(0, transform.position);
lineRender.SetPosition(1, Vector3.down * hit.distance);
}
}
}
}
第二点是
lineRender.SetPosition(1, transform.position + Vector3.down * hit.distance);
或
lineRender.SetPosition(1, ray.GetPoint(hit.distance));
或更简洁
lineRender.SetPosition(1, hit.point);
我想做的是在我使用光线距离和线渲染器的两个游戏对象之间画线,但线太长了。下面是我的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineTest : MonoBehaviour
{
RaycastHit hit;
public LineRenderer lineRender;
private void Awake()
{
lineRender = GetComponent<LineRenderer>();
}
void Start()
{
Ray ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider != null)
{
lineRender.enabled = true;
lineRender.SetPosition(0, transform.position);
lineRender.SetPosition(1, Vector3.down * hit.distance);
}
}
}
}
第二点是
lineRender.SetPosition(1, transform.position + Vector3.down * hit.distance);
或
lineRender.SetPosition(1, ray.GetPoint(hit.distance));
或更简洁
lineRender.SetPosition(1, hit.point);