LineRenderer的用法 射线检测的进一步用法

可以在同一个面板上有两种颜色的线

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class LineRenderText : MonoBehaviour
 6 {
 7 
 8     LineRenderer Line;
 9     public Material[] m=new Material[2];
10     public int num = 2;
11     public int num1 = 2;
12     public int CNum;
13     bool b = false;
14     int index = 1;
15     float time = 0;
16     LineRenderer line1;
17     Vector3 pos;
18     bool isChange = true;
19     // Use this for initialization
20     void Start()
21     {
22         Line = GetComponent<LineRenderer>();
23         Line.material = m[0];
24     }
25 
26     // Update is called once per frame
27     void Update()
28     {
29 
30         Line.SetPosition(0, Vector3.zero);
31         Line.SetPosition(1, new Vector3(10, 10, 10));
32         Line.SetWidth(0.1f, 1f);
33         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
34         RaycastHit hit;        
35         if (Physics.Raycast(ray, out hit))
36         {                         
37             if (Input.GetMouseButtonDown(0) && hit.collider.name == "Plane")
38             {
39                 index++;
40                 if (index % 2 == 0)
41                 {
42                     b = true;
43                 }
44                 else
45                 {
46                     pos = hit.point;
47                     b = false;                  
48                 }
49             }
50             if (b)
51             {
52                 time += Time.deltaTime;
53                 if (time > 0.06f)
54                 {
55                     if (isChange)
56                     {
57                         num++;
58                         Line.SetVertexCount(num);
59                         Line.SetPosition(num - 1, hit.point);
60                     }
61                     else
62                     {
63                         num1++;
64                         line1.SetVertexCount(num1);
65                         line1.SetPosition(num1 - 1, hit.point);
66                     }
67                    
68                     time = 0;
69                 }
70             }
71             if (!b)
72             {               
73                     if (hit.collider.name == "Cube1"&&Input.GetMouseButtonDown(1))
74                     {
75                         GameObject go = new GameObject();
76                         line1 = go.AddComponent<LineRenderer>();
77                         line1.material = m[1];
78                         print(hit.collider.name);
79                         line1.SetPosition(0, pos);
80                         line1.SetPosition(1, hit.point);
81                         isChange = false;
82                     }
83                 
84             }
85 
86 
87         }
88 
89         //Line.SetPositions();
90     }
91 }
LineRenderer画了两种颜色的线条

 

 

 

 

父子关系,找失活的儿子,transform.find();自

方向是自己的forword

 

点击的那个点:hit.point;

 

 

 它的缺点是只画方向

 

 定义层 

 

 

2.从cube发射射线 脚本挂cube,cube的z轴,,检测出来加入list,小怪走了就remove

 

 

2.1

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class RayText : MonoBehaviour {
 6     public LayerMask Layer;
 7     // Use this for initialization
 8     void Start () {
 9         
10     }
11     
12     // Update is called once per frame
13     void Update () {
14         Ray ray = new Ray(transform.position,transform.forward);
15         RaycastHit hit;
16         if (Physics.Raycast(ray,out hit))
17         {
18             Debug.DrawLine(transform.position,transform.forward,Color.gray,100);
19             if (hit.collider.name.Equals("Sphere"))
20             {
21                 hit.collider.GetComponent<MeshRenderer>().material.color = Color.green;
22             }
23         }
24 
25         //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
26         //RaycastHit hit;
27         //if (Physics.Raycast(ray,out hit,100,Layer))
28         //{
29         //    if (hit.collider.gameObject.name == "Cube")
30         //    {
31         //      Debug.DrawLine(transform.position,hit.point,Color.red,100);//运行状态下Scene的场景中划线
32         //        //hit.collider.gameObject.AddComponent<Rigidbody>();
33         //        //hit.collider.GetComponent<BoxCollider>().isTrigger = true;
34         //    }
35         //}
36 
37     }
38 }

 

 辅助工具

 

2.2重点

 

 获得新点:

点击就画一个点:

不用鼠标点击的时候就这样

 

 

转载于:https://www.cnblogs.com/satanj/p/9754468.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值