人生如白驹过隙,忽然而已|

云鲸啊

园龄:3年5个月粉丝:5关注:4

unity-射线检测(大佬代码的注释)及部分代码函数解释的具体链接

	private bool Look(StateController controller)
    {
        var defaultStats = controller.defaultStats;

        //一条向前的射线
        if (LookAround(controller, Quaternion.identity, Color.green))
            return true;

        //多一个精确度就多两条对称的射线,每条射线夹角是总角度除与精度
        float subAngle = (defaultStats.lookAngle / 2) / defaultStats.lookAccurate;
        for (int i = 0; i < defaultStats.lookAccurate; i++)
        {
            if (LookAround(controller, Quaternion.Euler(0, -1 * subAngle * (i + 1), 0), Color.green) 
                || LookAround(controller, Quaternion.Euler(0, subAngle * (i + 1), 0), Color.green))
                return true;
        }

        return false;
    }

    //射出射线检测是否有Player
    static public bool LookAround(StateController controller, Quaternion eulerAnger,Color DebugColor)
    {
        Debug.DrawRay(controller.eyes.position, eulerAnger * controller.eyes.forward.normalized * controller.defaultStats.lookRange, DebugColor);

        RaycastHit hit;
        if (Physics.Raycast(controller.eyes.position, eulerAnger * controller.eyes.forward, out hit, controller.defaultStats.lookRange) && hit.collider.CompareTag("Player"))//forward人物正前方(z轴方向)   controller.defaultStats.lookRange寻找范围 out hit为RaycastHit固定用法
        {
            controller.chaseTarget = hit.transform;
            return true;
        }
        return false;
    }

    public float accuracy = 1f;                     //检测精度

    private bool Look(StateController controller)
    {
        float subAngle = angle / accuracy;          //每条射线需要检测的角度范围,angle角度
        for (int i = 0; i < accuracy; i++)
            if (LookAround(controller, Quaternion.Euler(0, -angle / 2 + i * subAngle + Mathf.Repeat(rotatePerSecond * Time.time, subAngle), 0), distance, debugColor)) // Mathf.Repeat 求余数的函数 rotatePerSecond每秒旋转角度 Quaternion.Euler返回一个旋转,它围绕 z 轴旋转 z 度、围绕 x 轴旋转 x 度、围绕 y 轴旋转 y 度(按该顺序应用)。(官方api文档解释)
                return true;
        return false;
    }

射线检测实现方法:源码链接:https://blog.csdn.net/l773575310/article/details/73251093
forward链接为:https://blog.csdn.net/kaluluosi111/article/details/17206655
RaycastHit链接为:https://blog.csdn.net/qq_30454411/article/details/79140318

本文作者:云鲸啊

本文链接:https://www.cnblogs.com/yunjing/p/15869831.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   云鲸啊  阅读(396)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 ocean eyes bili
ocean eyes - bili
00:00 / 00:00
An audio error has occurred.

作词 : Finneas O'Connell

作曲 : Finneas O'Connell

I've been watching you

For some time

Can't stop staring

At those ocean eye

Burning cities

And napalm skies

Fifteen flares inside those ocean eyes

Your ocean eyes

No fair

You really know how to make me cry

When you give me those ocean eyes

I'm scared

I've never fallen from quite this high

Falling into your ocean eyes

Those ocean eyes

I've been walking through

A world gone blind

Can't stop thinking of your diamond mind

Can't stop thinking of your diamond mind

Careful creature

Careful creature

Made friends with time

He left her lonely with a diamond mind

He left her lonely with a diamond mind

And those ocean eyes

And those ocean eyes

No fair

You really know how to make me cry

You really know how to make me cry

When you give me those ocean eyes

I'm scared

I've never fallen from quite this high

I've never fallen from quite this high

Falling into your ocean eyes

Those ocean eyes

Those ocean eyes

No fair

No fair

You really know how to make me cry

You really know how to make me cry

When you give me those ocean eyes

I'm scared

I'm scared

I've never fallen from quite this high

I've never fallen from quite this high

Falling into your ocean eyes

Those ocean eyes

Those ocean eyes