unity 实现两点之间画线测距

Hierarchy面板设置如下:
Hierarchy中设置
效果如下:
在这里插入图片描述
脚本代码如下:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class HuaXianCeJu : MonoBehaviour
{
public static HuaXianCeJu instance;
public GameObject pointA;
public GameObject pointB;
public Transform fatherPointAB;
public Transform canvasDistance;//显示距离的面板组件
private bool hasTwoPoints = false;//判断是否有两个点
private LineRenderer m_lineRenderer;
private int sphereCount;
public TMP_Text text2PointMeter;

private void Awake()
{
    instance = this;
    m_lineRenderer = GetComponent<LineRenderer>();
}
void Start()
{
    //初始化时先隐藏创建的两个点和画线,
    HidePointAB();
    m_lineRenderer.GetComponent<LineRenderer>().enabled = false;
}
private void HidePointAB()
{
    foreach (Transform transformAB in fatherPointAB)
    {
        transformAB.gameObject.SetActive(false);
    }
    canvasDistance.gameObject.SetActive(false);
}
public void CreatPointAB()
{      
    if (Input.GetMouseButtonDown(0)) // 鼠标左键点击
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            //是UI的时候,执行相关的UI操作
            Debug.Log("是UI");
            return;
        }
        //每次点击前先隐藏两个点,等到需要时激活
        HidePointAB();
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit hit, maxDistance: 1000))
        {
            if (sphereCount == 0)
            {
                sphereCount = 1;
                pointA.gameObject.SetActive(true);
                pointA.GetComponent<Transform>().localScale = new Vector3(0.3f, 0.3f, 0.3f);
                pointA.transform.position = hit.point + new Vector3(0, 0.1f, 0);
                m_lineRenderer.GetComponent<LineRenderer>().enabled = false;
            }
            else if (sphereCount == 1)
            {
                sphereCount = 2;
                pointA.gameObject.SetActive(true);
                pointB.gameObject.SetActive(true);
                pointB.GetComponent<Transform>().localScale = new Vector3(0.3f, 0.3f, 0.3f);
                pointB.transform.position = hit.point + new Vector3(0, 0.1f, 0);
                m_lineRenderer.GetComponent<LineRenderer>().enabled = true;
                PointABMidPosition();
                hasTwoPoints = true;
            }
        }
    }
    if (hasTwoPoints && sphereCount == 2)
    {
        HuaXian();
        sphereCount = 0;
        hasTwoPoints = false;
    }
}
void Update()
{
    CreatPointAB();
}
private void HuaXian()
{
    m_lineRenderer.SetPosition(0, pointA.transform.position + new Vector3(0, 0f, 0));
    m_lineRenderer.SetPosition(1, pointB.transform.position + new Vector3(0, 0f, 0));
}
//计算两点之间的位置
private void PointABMidPosition()
{
    canvasDistance.gameObject.SetActive(true);
    canvasDistance.position = (pointA.transform.position + pointB.transform.position) / 2 + new Vector3(0.25f, 0.25f, 0);
    PointABDistance();
}
//计算两点之间的距离
float abDistance = 0;
private void PointABDistance()
{
    abDistance = Vector3.Distance(pointA.transform.position,pointB.transform.position);
    Debug.Log(abDistance);
    text2PointMeter.text ="两点之间的距离:"+ abDistance.ToString("f2")+"米";
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值