Unity3D-射线效果


基于airplane_02 下面新建 Line Renderer

将上面的几个地方设置下

添加Script脚本:

脚本代码为:

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

public class AirPlane : MonoBehaviour
{
    public LineRenderer lineRender;

    public Transform gunPoint;
    // Start is called before the first frame update
    void Start()
    {
        lineRender = GetComponent<LineRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            lineRender.enabled = true;
            lineRender.SetPosition(0, gunPoint.position);
            Ray ray = Camera.main.ScreenPointToPay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                lineRender.SetPosition(1, hit.point);
            }
        }
        else
        {
            lineRender.enabled = false;
        }

    }
}


在airplane_02下 Create Empty ,gunPoint

运行可看到效果:


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM