自学内容网 自学内容网

3.4射线

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

public class TestSheX : MonoBehaviour
{
    Camera camera;
    public LayerMask layerMask;
    // Start is called before the first frame update
    void Start()
    {
        // Debug.DrawLine(transform.position, transform.position + transform.forward);
        //参数:位置,目标位置,颜色,持续时间,是否遮挡
        //Debug.DrawLine(transform.position, transform.position + transform.forward,Color.red,10);
        //Debug.DrawLine(transform.position, transform.position + transform.forward,Color.red,10,false);
        //Debug.DrawRay(transform.position, transform.forward,Color.red,10,true);

        //camera = GameObject.Find("Camera").GetComponent<Camera>();
        //layerMask = LayerMask.GetMask("boss");

        Collider[] colliders=Physics.OverlapSphere(transform.position,100f);
        for(int i = 0; i < colliders.Length; i++)
        {
            Debug.Log("碰撞了" + colliders.Length + "个物体");
            Debug.Log("碰撞了" + colliders[i].gameObject.name + "个物体");
        }
     }

    // Update is called once per frame
    void Update()
    {
        //参数:位置,方向,颜色,持续时间,是否遮挡
        // Debug.DrawRay(transform.position, transform.forward, Color.red, 10, true);
        //if (Input.GetMouseButtonDown(0))
        //{
        //    //返回一条射线
        //    Ray ray = camera.ScreenPointToRay(Input.mousePosition);
        //    {
        //        RaycastHit hit;

        //        if (Physics.Raycast(ray, out hit, 1000f, layerMask))
        //没有过滤图层
        //        if (Physics.Raycast(ray, out hit, 1000f))
        //        {
        //            Debug.Log("命中了碰撞:" + hit.collider.gameObject.name +"坐标点"+hit.point);
        //            Destroy(hit.collider.gameObject);
        //        }
        //        else
        //        {
        //            Debug.Log("未命中了碰撞");
        //        }
        //    }
        // }


        //if (Input.GetMouseButtonDown(0))
        //{
        //    //返回一条射线
        //    Ray ray = camera.ScreenPointToRay(Input.mousePosition);
        //    {
        //        RaycastHit[] hit=Physics.RaycastAll(transform.position,transform.forward,1000f);

        //        for(int i = 0; i < hit.Length; i++)
        //        {
        //            Debug.Log("碰撞到了:" + hit.Length + "个物体");
        //            Debug.Log("命中了碰撞第" + i + "个物体:" + hit[i].collider.gameObject.name + "坐标点" + hit[i].point);
        //        }

        //        //if (Physics.Raycast(ray, out hit, 1000f, layerMask))
        //        //{
        //        //    Debug.Log("命中了碰撞:" + hit.collider.gameObject.name + "坐标点" + hit.point);
        //        //    Destroy(hit.collider.gameObject);
        //        //}
        //        //else
        //        //{
        //        //    Debug.Log("未命中了碰撞");
        //        //}
        //    }
        //}
    }
}

原文地址:https://blog.csdn.net/qq_30003129/article/details/143091589

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!