基于airplane_02 下面新建 Line Renderer
将上面的几个地方设置下
添加Script脚本:
脚本代码为:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AirPlane : MonoBehaviour
{
public LineRenderer lineRender;
public Transform gunPoint;
// Start is called before the first frame update
void Start()
{
lineRender = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
lineRender.enabled = true;
lineRender.SetPosition(0, gunPoint.position);
Ray ray = Camera.main.ScreenPointToPay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
lineRender.SetPosition(1, hit.point);
}
}
else
{
lineRender.enabled = false;
}
}
}
在airplane_02下 Create Empty ,gunPoint
运行可看到效果: