CultivationOfBrewing-2/Assets/Scripts/HQB/RangeFinding/RangeFinding.cs

146 lines
5.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Unity.Burst.CompilerServices;
using UnityEngine;
//测量距离
public class RangeFinding : MonoBehaviour
{
//总控制
public bool isClbool;
private Vector3 posOne, posTwo;
//测量控制
public bool isOpenDistance;
private int distanceInt; //计数控制
public Transform prefabTransform; //测量的预制体
private Transform myDistanceObj;
public Transform allCLParentTransform; //所有预制体生成的父节点
protected SorghumController Sorg_posOne;
protected SorghumController Sorg_posTwo;
// Start is called before the first frame update
void Start()
{
}
private void OnDisable()
{
while (allCLParentTransform.childCount > 0)
{
DestroyImmediate(allCLParentTransform.GetChild(0).gameObject);
}
}
// Update is called once per frame
void Update()
{
if (isClbool)
{
try
{
if (Input.GetMouseButtonDown(0))
{
Ray ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit1;
if (Physics.Raycast(ray1, out hit1, 1000))
{
if (hit1.collider.gameObject.GetComponent<SorghumController>() == null)
return;
posOne = Input.mousePosition;
}
else
{
return;
}
}
Ray ray2 = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit2;
//距离
if (isOpenDistance)
{
if (Input.GetMouseButtonUp(0))
{
posTwo = Input.mousePosition;
if (Physics.Raycast(ray2, out hit2, 1000))
{
if (hit2.collider.gameObject.GetComponent<SorghumController>() == null)
return;
if (distanceInt == 0)
{
if (allCLParentTransform.childCount > 0)
{
while (allCLParentTransform.childCount > 0)
{
DestroyImmediate(allCLParentTransform.GetChild(0).gameObject);
}
Sorg_posOne = null;
Sorg_posTwo = null;
}
Sorg_posOne = hit2.collider.gameObject.GetComponent<SorghumController>();
distanceInt++;
//鼠标点击克隆物体
myDistanceObj = Instantiate(prefabTransform, allCLParentTransform);
// transform.TransformPoint(Prefab,hit.poit, Quaternion.identity);
myDistanceObj.transform.GetChild(0).position = hit2.point;
}
else
{
Sorg_posTwo = hit2.collider.gameObject.GetComponent<SorghumController>();
if (Sorg_posOne == Sorg_posTwo)
{
Transform low_P = Sorg_posOne.transform.Find("高粱/lowPoint");
Transform high_P = Sorg_posOne.transform.Find("高粱/highPoint");
myDistanceObj.transform.GetChild(0).position = low_P.position;
myDistanceObj.transform.GetChild(1).position = high_P.position;
}
else
{
Transform low_P = Sorg_posTwo.transform.Find("高粱/highPoint");
Transform high_P = Sorg_posOne.transform.Find("高粱/highPoint");
myDistanceObj.transform.GetChild(0).position = low_P.position;
myDistanceObj.transform.GetChild(1).position = high_P.position;
}
//isOpenJL = false;
distanceInt = 0;
}
}
else
{
return ;
}
}
if (distanceInt > 0)
{
if (Physics.Raycast(ray2, out hit2, 1000))
{
myDistanceObj.transform.GetChild(1).position = hit2.point;
}
}
}
}
catch (System.Exception e)
{
Debug.LogException(e);
}
}
}
}