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