如何根据光线命中数使用linerenderer绘制多条线?

时间:2018-05-01 18:26:43

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateObjects : MonoBehaviour
{
    public GameObject[] objectsToRotate;
    public Transform to;
    public float speed = 0.1f;
    public Vector3 spinDirection;
    public bool useMouse = false;
    public bool useQuaternion = false;
    public bool nonstopSpin = false;

    Ray ray;
    RaycastHit hit;
    RaycastHit[] hits;

    GameObject[] myLines;
    LineRenderer lr;

    // Use this for initialization
    void Start()
    {
        myLines = new GameObject[objectsToRotate.Length];
        GenerateLinerenderer();

        if (objectsToRotate.Length > 0)
        {
            if (useQuaternion == true)
            {
                for (int i = 0; i < objectsToRotate.Length; i++)
                {
                    objectsToRotate[i].transform.rotation = Quaternion.Lerp(objectsToRotate[i].transform.rotation, to.rotation, Time.time * speed);
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (objectsToRotate.Length > 0)
        {
            for (int i = 0; i < objectsToRotate.Length; i++)
            {
                /*ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit))
                {
                    Debug.DrawLine(Input.mousePosition, hit.transform.position, Color.red);

                    if (hit.collider.name == objectsToRotate[i].name)
                        objectsToRotate[i].transform.Rotate(1, 1, 1);
                }*/


                hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition), 100.0f);

                for (int x = 0; x < hits.Length; x++)
                {
                    RaycastHit hit = hits[x];

                    if (hit.collider.name == objectsToRotate[i].name)
                    {
                        objectsToRotate[i].transform.Rotate(1, 1, 1);
                        Debug.DrawLine(Input.mousePosition, hit.transform.position, Color.red);
                        SpawnLineGenerator(Input.mousePosition, hit.transform.position);
                    }
                }

                if (useMouse == true)
                {
                    if (Input.GetMouseButton(0))
                    {
                        Rotate(i);
                    }
                }
                else
                {
                    Rotate(i);
                }
            }
        }
    }

    private void Rotate(int i)
    {
        if (useQuaternion == true)
        {
            objectsToRotate[i].transform.rotation = Quaternion.Lerp(objectsToRotate[i].transform.rotation, to.rotation, Time.time * speed);
        }

        if (nonstopSpin == true)
        {
            objectsToRotate[i].transform.Rotate(1, 1, 1);
        }
    }

    void SpawnLineGenerator(Vector3 start, Vector3 end)
    {
        lr.SetPosition(0, start);
        lr.SetPosition(1, end);
    }

    private void GenerateLinerenderer()
    {
        for (int i = 0; i < myLines.Length; i++)
        {
            myLines[i] = new GameObject();
            myLines[i].name = "FrameLine";

            myLines[i].AddComponent<LineRenderer>();
            lr = myLines[i].GetComponent<LineRenderer>();
            lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
            lr.startColor = Color.green;
            lr.useWorldSpace = false;
            lr.endColor = Color.green;
            lr.startWidth = 0.1f;//0.03f;
            lr.endWidth = 0.1f;//0.03f;
            lr.numCapVertices = 5;
        }
    }
}

例如,如果我同时在某些物体中使用光线,它会使用调试从鼠标位置到命中对象绘制多条红线:

Debug.DrawLine(Input.mousePosition,hit.transform.position,Color.red); 我想用linerenderer做同样的结果。但即使我碰到多个物体,它也会画出一条绿线。

0 个答案:

没有答案