LineRenderer初探

LineRenderer初探_第1张图片

如图给角色绘制游戏中可见的视野范围:

using UnityEngine;
using System.Collections;

public class LineRenderSee : MonoBehaviour {
    public float fieldOfViewAngle = 90;
    public float viewDistance = 10;
    public Color theColor = Color.yellow;
    public float lineWidth = 0.1f;

    private LineRenderer lineRenderer;
    // Use this for initialization
    void Start () {
        lineRenderer = gameObject.AddComponent();
        //设置材质
        lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        //设置点的最大值, 如果大于实际使用的点, 会把剩余的点设置成Vector3.zero
        lineRenderer.SetVertexCount (11); 
        //设置颜色
        lineRenderer.SetColors(theColor, theColor);
        //设置宽度
        lineRenderer.SetWidth(lineWidth, lineWidth);
    }
    
    // Update is called once per frame
    void Update () {

        Quaternion r1 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y - fieldOfViewAngle / 2f, transform.rotation.eulerAngles.z);
        Quaternion r2 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y - fieldOfViewAngle / 3.5f, transform.rotation.eulerAngles.z);
        Quaternion r3 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y - fieldOfViewAngle / 6f, transform.rotation.eulerAngles.z);
        Quaternion r4 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y - fieldOfViewAngle / 8f, transform.rotation.eulerAngles.z);
        Quaternion r5 = transform.rotation;
        Quaternion r6 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + fieldOfViewAngle / 8f, transform.rotation.eulerAngles.z);
        Quaternion r7 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + fieldOfViewAngle / 6f, transform.rotation.eulerAngles.z);
        Quaternion r8 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + fieldOfViewAngle / 3.5f, transform.rotation.eulerAngles.z);
        Quaternion r9 = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + fieldOfViewAngle / 2f, transform.rotation.eulerAngles.z);
        Vector3 f1 = transform.position + r1 * Vector3.forward * viewDistance;
        Vector3 f2 = transform.position + r2 * Vector3.forward * viewDistance;
        Vector3 f3 = transform.position + r3 * Vector3.forward * viewDistance;
        Vector3 f4 = transform.position + r4 * Vector3.forward * viewDistance;
        Vector3 f5 = transform.position + r5 * Vector3.forward * viewDistance;
        Vector3 f6 = transform.position + r6 * Vector3.forward * viewDistance;
        Vector3 f7 = transform.position + r7 * Vector3.forward * viewDistance;
        Vector3 f8 = transform.position + r8 * Vector3.forward * viewDistance;
        Vector3 f9 = transform.position + r9 * Vector3.forward * viewDistance;
        lineRenderer.SetPosition (0, transform.position);
        lineRenderer.SetPosition (1, f1);
        lineRenderer.SetPosition (2, f2);
        lineRenderer.SetPosition (3, f3);
        lineRenderer.SetPosition (4, f4);
        lineRenderer.SetPosition (5, f5);
        lineRenderer.SetPosition (6, f6);
        lineRenderer.SetPosition (7, f7);
        lineRenderer.SetPosition (8, f8);
        lineRenderer.SetPosition (9, f9);
        lineRenderer.SetPosition (10, transform.position);
    }
}


你可能感兴趣的:(LineRenderer初探)