使用射线投射从一个物体发射多束激光

时间:2019-01-15 06:34:07

标签: c# unity3d raycasting

我有一个想要多束激光束而不是一个的物体。

enter image description here

以下代码仅在一侧显示了线束。但从各方面都可以期待。

using UnityEngine;
using System.Collections;

public class Laser : MonoBehaviour
{

    private LineRenderer lr;
    private bool clicked = false;
    RaycastHit rh;

    // Use this for initialization
    void Start()
    {
        lr = GetComponent<LineRenderer>();

    }




    // Update is called once per frame
    void Update()
    {


        if (clicked)
        {

            lr.SetPosition(0, transform.position + new Vector3(0, 0, 0));
            RaycastHit hit;

            if (Physics.Raycast(transform.position + new Vector3(0, 0, 0), -transform.up, out hit))
            {
                if (hit.collider)
                {
                    lr.SetPosition(1, hit.point);
                }
            }
            else lr.SetPosition(1, -transform.up * 5000);


            if (Physics.Raycast(transform.position + new Vector3(0, 0, 0), transform.up, out hit))
            {
                if (hit.collider)
                {
                    lr.SetPosition(1, hit.point);
                }
            }
            else lr.SetPosition(1, transform.up * 5000);

           if (Physics.Raycast(transform.position + new Vector3(0, 0, 0), transform.right, out hit))
            {
                if (hit.collider)
                {
                    lr.SetPosition(1, hit.point);
                }
            }
            else lr.SetPosition(1, transform.right * 5000);

           if (Physics.Raycast(transform.position + new Vector3(0, 0, 0), -transform.right, out hit))
            {
                if (hit.collider)
                {
                    lr.SetPosition(1, hit.point);
                }
            }
            else lr.SetPosition(1, -transform.right * 5000);


        }

    }
}

1 个答案:

答案 0 :(得分:1)

public LineRenderer lr_up;
public LineRenderer lr_down;
public LineRenderer lr_left;
public LineRenderer lr_right;

private bool clicked = false;

RaycastHit rh;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    if (clicked)
    {
        lr_up.SetPosition(0, transform.position);
        lr_down.SetPosition(0, transform.position);
        lr_left.SetPosition(0, transform.position);
        lr_right.SetPosition(0, transform.position);

        RaycastHit hit;

        if (Physics.Raycast(transform.position, -transform.up, out hit))
        {
            if (hit.collider)
            {
                lr_up.SetPosition(1, hit.point);
            }
        }
        else lr_up.SetPosition(1, -transform.up * 5000);


        if (Physics.Raycast(transform.position, transform.up, out hit))
        {
            if (hit.collider)
            {
                lr_down.SetPosition(1, hit.point);
            }
        }
        else lr_down.SetPosition(1, transform.up * 5000);

        if (Physics.Raycast(transform.position, transform.right, out hit))
        {
            if (hit.collider)
            {
                lr_left.SetPosition(1, hit.point);
            }
        }
        else lr_left.SetPosition(1, transform.right * 5000);

        if (Physics.Raycast(transform.position, -transform.right, out hit))
        {
            if (hit.collider)
            {
                lr_right.SetPosition(1, hit.point);
            }
        }
        else lr_right.SetPosition(1, -transform.right * 5000);
    }
}

这是您更正的代码。您将需要四个线渲染器来表示四个光束。您不能具有一个线条渲染器,并且不能期望它朝四个方向射击。在当前对象中创建四个空子级。分配线渲染器组件,并在脚本中为其分配各自的线渲染器。代码的其余部分将负责设置光束。