108 lines
4.0 KiB
C#
108 lines
4.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// 无人机规划路径
|
|
/// </summary>
|
|
public class ObjectPlanner : MonoBehaviour
|
|
{
|
|
public bool isPathCanBePlanned = false;//是否可以规划路径
|
|
public Camera camera; // Orthographic相机
|
|
public GameObject selectedObject; // 被选择的物体
|
|
public LineRenderer lineRenderer; // 用于绘制路线的线条
|
|
private bool isPlanning = false; // 是否正在规划路线
|
|
public Vector3[] positions;//已有路径
|
|
/// <summary>
|
|
/// 飞行速度
|
|
/// </summary>
|
|
public float FireSpeed = 20.0f;
|
|
void Start()
|
|
{
|
|
if (camera == null)
|
|
camera = Camera.main;
|
|
lineRenderer = GetComponent<LineRenderer>(); // 获取LineRenderer组件
|
|
|
|
// 设置线条材质和宽度
|
|
//lineRenderer.material = new Material(Shader.Find("Sprites/Default")); // 设置线条材质
|
|
lineRenderer.startWidth = 10f; // 设置线条起始宽度
|
|
lineRenderer.endWidth =10f; // 设置线条结束宽度
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isPathCanBePlanned)
|
|
{
|
|
if (Input.GetMouseButtonDown(1)) // 检测鼠标左键是否按下
|
|
{
|
|
Ray ray = camera.ScreenPointToRay(Input.mousePosition); // 从相机发射一条射线
|
|
if (Physics.Raycast(ray, out RaycastHit hit)) // 检测是否射中物体
|
|
{
|
|
if (selectedObject != null && !isPlanning) // 如果已选择物体并且没有正在规划路线
|
|
{
|
|
isPlanning = true; // 标记为正在规划路线
|
|
|
|
lineRenderer.positionCount = 1; // 设置线条的起点
|
|
lineRenderer.SetPosition(0, selectedObject.transform.position);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if (isPlanning && Input.GetMouseButton(1)) // 如果正在规划路线并且鼠标左键持续按下
|
|
{
|
|
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
|
|
if (Physics.Raycast(ray, out RaycastHit hit))
|
|
{
|
|
lineRenderer.positionCount++; // 增加线条顶点数
|
|
lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point); // 更新线条的顶点
|
|
}
|
|
}
|
|
|
|
if (isPlanning && Input.GetMouseButtonUp(1)) // 如果正在规划路线并且鼠标左键释放
|
|
{
|
|
isPlanning = false; // 停止规划路线
|
|
|
|
positions = new Vector3[lineRenderer.positionCount]; // 创建用于存储顶点坐标的数组
|
|
lineRenderer.GetPositions(positions); // 获取线条的顶点坐标
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置选定无人机编队
|
|
/// </summary>
|
|
/// <param name="_selectedObject"></param>
|
|
public void SetSelectedObject(GameObject _selectedObject)
|
|
{
|
|
selectedObject = _selectedObject; // 选定新的物体
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 按规划路径开始移动
|
|
/// </summary>
|
|
public void StartMoveObjectAlongPath()
|
|
{
|
|
if (positions.Length > 0)
|
|
{
|
|
StartCoroutine(MoveObjectAlongPath(positions)); // 启动协程,按规划的路线移动物体
|
|
}
|
|
}
|
|
|
|
IEnumerator MoveObjectAlongPath(Vector3[] positions) // 协程:按路线移动物体
|
|
{
|
|
for (int i = 0; i < positions.Length; i++) // 迭代线条的顶点坐标
|
|
{
|
|
Vector3 targetPosition = positions[i] + new Vector3(0, 10, 0);// 目标位置为当前顶点坐标
|
|
while (selectedObject.transform.position != targetPosition) // 当物体未到达目标位置时
|
|
{
|
|
selectedObject.transform.position = Vector3.MoveTowards(selectedObject.transform.position, targetPosition, Time.deltaTime * FireSpeed); // 平滑移动物体
|
|
yield return null; // 等待一帧时间
|
|
}
|
|
yield return null; // 等待一帧时间
|
|
}
|
|
}
|
|
}
|