LineRenderer初探

weixin_34146805發表於2016-10-04
1537644-6586425bbc0bc127.png

如圖給角色繪製遊戲中可見的視野範圍:

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>();
        //設定材質
        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);
    }
}