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重點

 

 獲得新點:

點擊就畫一個點:

不用鼠標點擊的時候就這樣

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM