如何在Unity中画抛物线

 1 using UnityEngine;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 [ExecuteInEditMode]
 5 public class Parabola : MonoBehaviour {
 6     //重力
 7     [Range(0,1)]
 8     public float gravity = 0.13f;
 9     //最大长度
10     public float maxLength = 50;
11     //两点之间的距离
12     const float length = 0.2f;
13     //点集合
14     List<Vector3> m_List = new List<Vector3>();
15     Material m_LineMat;
16 
17     public void OnRenderObject() {
18         CreateLineMaterial();
19         m_LineMat.SetPass(0);
20 
21         Vector3 position = transform.position;
22         Vector3 forward = transform.rotation * Vector3.forward * length;
23         Vector3 newPos = position;
24         Vector3 lastPos = newPos;
25         m_List.Add(newPos);
26         int i = 0, iMax = 0;
27         float dis = 0;
28         while (dis < maxLength) {
29             i++;
30             newPos = lastPos + forward + Vector3.up * i * -gravity * 0.1f;
31             dis += Vector3.Distance(lastPos, newPos);
32             m_List.Add(newPos);
33             lastPos = newPos;
34         }
35 
36         GL.Begin(GL.LINES);
37         i = 0;
38         iMax = m_List.Count;
39         for (i = 0; i < iMax; i++) {
40             GL.Vertex(m_List[i]);
41         }
42         GL.End();
43 
44         m_List.Clear();
45     }
46 
47     void CreateLineMaterial() {
48         if (!m_LineMat) {
49             var shader = Shader.Find("Hidden/Internal-Colored");
50             m_LineMat = new Material(shader);
51             m_LineMat.hideFlags = HideFlags.HideAndDontSave;
52             m_LineMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
53             m_LineMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
54             m_LineMat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
55             m_LineMat.SetInt("_ZWrite", 0);
56         }
57     }
58 }
View Code

 

原文:http://blog.csdn.net/utilxk

posted @ 2017-12-27 17:20  lovewaits  阅读(1067)  评论(0编辑  收藏  举报