当前位置: 首页 > article >正文

Unity 小功能

1.物体a看向物体b方向,而忽略b的高度

   private void LookTargetDirection(Transform target)
    {
        var dic = (target.position - transform.position).normalized;
        dic.y = 0;
        if (dic != Vector3.zero)
        {
            Quaternion targetRotation = Quaternion.LookRotation(dic);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 20f); //看向目标
        }
    }

2.延迟调用方法

  public void WaitActionTime(float timer, Action act)
    {
        StartCoroutine(DelayAction(timer, act));
    }

    public IEnumerator DelayAction(float timer, Action callback)
    {
        yield return new WaitForSeconds(timer);

        callback?.Invoke();

    }

3.代码指定动画添加事件

  public void GetAnim(string aniName)
    {
        // 获取动画控制器中的所有动画剪辑
        RuntimeAnimatorController runtimeController = ani.runtimeAnimatorController;
        if (runtimeController == null)
        {
            Debug.LogError("Animator Controller 未找到!");
            return;
        }

        AnimationClip[] animationClips = runtimeController.animationClips;
        if (animationClips == null || animationClips.Length == 0)
        {
            Debug.LogError("未找到动画剪辑!");
            return;
        }

        // 遍历动画剪辑
        foreach (AnimationClip clip in animationClips)
        {
            Debug.Log("找到动画剪辑: " + clip.name);

            // 如果找到目标动画剪辑
            if (clip.name == aniName)
            {
                // 添加动画事件
                AddEventToClip(clip);
                Debug.Log("已为动画剪辑 " + clip.name + " 添加动画事件");
            }
        }
    }

    // 添加动画事件
    void AddEventToClip(AnimationClip clip)
    {
        // 创建动画事件
        AnimationEvent animationEvent = new AnimationEvent
        {
            time = 0.7f, // 事件触发时间(秒)
            functionName = "OnAnimationEvent", // 回调函数名称
            stringParameter = "Hello from Animation Event!" // 传递的参数
        };

        // 将事件添加到动画剪辑
        clip.AddEvent(animationEvent);
    }

    // 动画事件回调函数
    void OnAnimationEvent(string message)
    {
        Debug.Log("动画事件触发: " + message);
        StoneMove();
    }

4.距离范围检测

  bool IsTargetInRange(float range)
    {
        float distance = Vector3.Distance(transform.position, target.position);
        return distance <= range;
    }

5.编辑器选中物体并绘制范围线条

  void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red; // 设置 Gizmos 的颜色
        Gizmos.DrawWireSphere(transform.position, range); // 绘制一个半径为 range 米的线框球体

    }

6.范围内施加爆炸力

   void Explode()
    {
        // 检测以物体为中心,半径为 explosionRadius 的周围所有碰撞体
        Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius);

        foreach (Collider collider in colliders)
        {
            Rigidbody rb = collider.GetComponent<Rigidbody>();  // 获取碰撞体的 Rigidbody 组件
        
                // 对周围物体施加爆炸力
                rb.constraints = RigidbodyConstraints.None;
                rb.useGravity = true;
                rb.AddExplosionForce(100, transform.position, 10, 1, ForceMode.Impulse);
   
          
        }
    }


http://www.kler.cn/a/574587.html

相关文章:

  • Linux 开发工具
  • 【微知】Mellanox驱动中to是什么?有哪些超时时间?(time out,心跳2s,reset 1分钟)
  • Docker的常用镜像
  • 如何将一台服务器的pip环境迁移到另一个机器?
  • MoE 架构:专家齐聚,智启未来 —— 解锁传统稠密模型的瓶颈
  • 某书x-s参数更新自动化获取密钥iv脚本
  • 网页制作11-html,css,javascript初认识のCCS样式列表(下)
  • Go学习笔记:基础语法6
  • 庭田科技携手西门子工业软件成功举办振动噪声技术研讨会
  • 从“0”开始入门PCB之(5)完结篇!--快速入门原理图DRC,PCB的符号与布局,2D与3D效果,PCB的图层和布线,PCB板框
  • 攻防世界WEB(新手模式)19-file_include
  • 机器视觉运动控制一体机在天地盖同步跟随贴合解决方案
  • 三数之和~
  • 快手,蓝禾,得物,优博讯,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮25届春招内推
  • std::string的模拟实现
  • Windows 图形显示驱动开发-WDDM 3.2-本机 GPU 围栏对象(三)
  • 数据结构与算法:希尔排序
  • HTML 编辑器推荐与 VS Code 使用教程
  • springcloud智慧工地物联网云管理系统源码
  • LeetCode 双指针章节