使用LineRenderer画出一条线,摄像机根据线的方向旋转

    LineRenderer画线功能很简单,在一个空物体上添加LineRenderer组件,然后主要使用LineRenderer.SetPosition(0, StartPos)和 LineRenderer.SetPosition(1, EndPos);两句话画线,LineRenderer.startWidth = Width和LineRenderer.endWidth = Width;这两句话改变线条的宽度。

  有一点要注意,之前看到谁的脚本使用代码添加材质,在编辑器下可以画线,但是打包发布之后就会发现画线不能使用(当时还把我找了好长时间)。

这样一来我就写上完整的画线脚本就出来了:

/// <summary>
    /// 画线,通视
    /// </summary>
    /// <param name="pos"></param>
    private void DrawLine(Vector3 pos)
    {
        //Debug.Log("huaxian");
        if (!DrawCamera.gameObject.activeSelf)
        {
            DrawCamera.gameObject.SetActive(true);
        }
        //起点设置
       // line.material = new Material(Shader.Find("Particles/Additive"));
        line.enabled = true;
        text.enabled = true;
        line.startWidth = Width;
        line.SetPosition(0, StartPos);
        line.endWidth = Width;
        //终点设置
        EndPos = new Vector3(pos.x, pos.y+offset, pos.z);
        line.SetPosition(1, EndPos);

        line.startWidth = Width;
        line.endWidth = Width;
        //距离以及提示设置
        float Distance = Vector3.Distance(StartPos, EndPos);
		message += "text:";
        text.text = "距离:" + Mathf.Round(Distance).ToString() + "米";
        text.transform.position = Camera.main.WorldToScreenPoint(GetBetweenPoint(StartPos, EndPos, 0.5f));
        //摄像机位置方向设置
        //DrawCamera.enabled = true;
        DrawCamera.transform.position = StartPos;
        DrawCamera.transform.LookAt(EndPos);
    }

我这里初始坐标是在前面获取好的,终点坐标是在后面根据鼠标移动位置赋予。

这两个坐标的获取,我这里是以点击的位置方式为开始坐标,然后开启通视模式,再次点击通视结束。使用射线检测获取结束坐标(射线检测真好用),但是同样会有问题,当时射线检测不到物体的时候,画线则会以(0,0,0)位置为终点坐标(这里可能是因为该点挂脚本位置的坐标也可能是射线没有检测到物体时的坐标),所以我们要考虑好这种情况下该怎么赋予终点坐标。

代码如下:

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 Pos;
        RaycastHit Hit;
        if (Physics.Raycast(ray, out Hit))
        {
            GameObject gameObject = Hit.collider.gameObject;
            Pos = Hit.point;
            //if (EventSystem.current.IsPointerOverGameObject())
            //{
            //    Isdraw = false;
            //}
            if (Input.GetMouseButtonUp(0))
            {
                if (EventSystem.current.IsPointerOverGameObject()) return;
                //if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) return;
                Isdraw = !Isdraw;
                //Debug.Log(Isdraw);
                //因为地形没有添加collider,底下有个Plane所以才能画线,所以添加上偏移,防止视角在地下
                StartPos = new Vector3(Hit.point.x, gameObject.transform.position.y + offset, Hit.point.z);
            }

        }

        else
        {
            float distance = Camera.main.transform.position.y-line.transform.position.y;
            Pos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, distance));

        }

这样就差不多了。

具体的完整代码如下:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class LineRendererManager : MonoBehaviour 
{
    //起点坐标
    public Vector3 StartPos;
    //终点坐标
    public Vector3 EndPos;
    //线条组件
    private LineRenderer line;
    //是否处于画线状态
    public bool Isdraw = false;

    //通视摄像机
    public Camera DrawCamera;
    //距离提示
    public Text text;
    //偏移量
    public float offset=0.4f;
    public Text tips;

	private string message = "等待操作";


    // Use this for initialization
    void Start () 
	{
        //ScreenShotCamera = GameObject.Find("ScreenShotCamera").gameObject;
        c = GameObject.Find("UI_Root").GetComponent<Canvas>();
        line = this.transform.GetComponent<LineRenderer>();
        Debug.Log(line);
        text.enabled = false;
        line.enabled = false;
        message = "line:"+line;
    }
	
	// Update is called once per frame
	void Update () 
	{
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 Pos;
        RaycastHit Hit;
        if (Physics.Raycast(ray, out Hit))
        {
            GameObject gameObject = Hit.collider.gameObject;
            Pos = Hit.point;
            //if (EventSystem.current.IsPointerOverGameObject())
            //{
            //    Isdraw = false;
            //}
            if (Input.GetMouseButtonUp(0))
            {
                if (EventSystem.current.IsPointerOverGameObject()) return;
                //if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0) return;
                Isdraw = !Isdraw;
                //Debug.Log(Isdraw);
                //因为地形没有添加collider,底下有个Plane所以才能画线,所以添加上偏移,防止视角在地下
                StartPos = new Vector3(Hit.point.x, gameObject.transform.position.y + offset, Hit.point.z);
            }

        }

        else
        {
            float distance = Camera.main.transform.position.y-line.transform.position.y;
            Pos = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, distance));

        }

        if (Input.GetMouseButton(1))
        {
            Isdraw = false;
        }
        if (Isdraw)
        {
            DrawLine(Pos);

        }
        else
        {
            if (line == null) return;
            line.enabled = false;
            text.enabled = false;
        }
        
    }
    /// <summary>
    /// 看向线终点位置
    /// </summary>
    /// <param name="pos">终点位置</param>
    public void LookEndPos(Vector3 pos)
    {
        DrawCamera.transform.LookAt(pos);
    }

    /// <summary>
    /// 获取两点之间距离一定百分比的一个点
    /// </summary>
    /// <param name="start">起始点</param>
    /// <param name="end">结束点</param>
    /// <param name="distance">起始点到目标点距离百分比</param>
    /// <returns></returns>
    private Vector3 GetBetweenPoint(Vector3 start, Vector3 end, float percent)
    {
        Vector3 normal = (end - start).normalized;
        float distance = Vector3.Distance(start, end);
        return normal * (distance * percent) + start;
    }
    public float Width = 0.1f;
    /// <summary>
    /// 画线,通视
    /// </summary>
    /// <param name="pos"></param>
    private void DrawLine(Vector3 pos)
    {
        //Debug.Log("huaxian");
        if (!DrawCamera.gameObject.activeSelf)
        {
            DrawCamera.gameObject.SetActive(true);
        }
        //起点设置
       // line.material = new Material(Shader.Find("Particles/Additive"));
        line.enabled = true;
        text.enabled = true;
        line.startWidth = Width;
        line.SetPosition(0, StartPos);
        line.endWidth = Width;
        //终点设置
        EndPos = new Vector3(pos.x, pos.y+offset, pos.z);
        line.SetPosition(1, EndPos);

        line.startWidth = Width;
        line.endWidth = Width;
        //距离以及提示设置
        float Distance = Vector3.Distance(StartPos, EndPos);
		message += "text:";
        text.text = "距离:" + Mathf.Round(Distance).ToString() + "米";
        text.transform.position = Camera.main.WorldToScreenPoint(GetBetweenPoint(StartPos, EndPos, 0.5f));
        //摄像机位置方向设置
        //DrawCamera.enabled = true;
        DrawCamera.transform.position = StartPos;
        DrawCamera.transform.LookAt(EndPos);
    }


    /// <summary>
    /// 隐藏状态时将Isdrag初始化
    /// </summary>
    private void OnEnable()
    {
        Isdraw = false;     
    }


   

}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值