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

unity曲线射击

请添加图片描述
在这里插入图片描述
在这里插入图片描述
b站教程

using UnityEngine;
using System.Collections;public class BallLauncher : MonoBehaviour {public float m_R;public NewBullet m_BulletPre;public Transform m_Target;private void Start(){StartCoroutine(Attack());}private void OnDestroy(){StopAllCoroutines();}private IEnumerator Attack(){while(true){NewBullet bullet = Instantiate(m_BulletPre, transform.position, Quaternion.identity);bullet.Attack(transform.position, GetRandomPoint(m_R), m_Target);yield return new WaitForSeconds(0.1f);yield return null;}}private Vector3 GetRandomPoint(float r){float x = Random.Range(-r, r);float ydis = m_Target.position.y - transform.position.y;float y = Random.Range(1, ydis);float z = Random.Range(-r, r);Vector3 vector3 = new Vector3(x, y, 0);return transform.position + vector3;}
}

第一种教程里面的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Bullet : MonoBehaviour
{public float speed = 5f;private void Start(){Destroy(gameObject,15f);}private void OnDestroy(){StopAllCoroutines();}public void Attack(Vector3 start, Vector3 midPoint, Transform target){StartCoroutine(Move(start, midPoint, target));}private IEnumerator Move(Vector3 start, Vector3 midPoint, Transform target){for (float i = 0; i <= 1f; i += Time.deltaTime){Vector3 p1 = Vector3.Lerp(start, midPoint, i);Vector3 p2 = Vector3.Lerp(midPoint, target.position, i);Vector3 p = Vector3.Lerp(p1, p2, i);//让子弹移动到p点yield return StartCoroutine(MoveToPoint(p));}yield return StartCoroutine(MoveToObject(target));}private IEnumerator MoveToPoint(Vector3 p){yield return null;while (Vector3.Distance(transform.position, p) > 0.1f){Vector3 dir = p - transform.position;transform.up = dir;transform.position = Vector3.MoveTowards(transform.position, p, Time.deltaTime * speed);yield return null;}}private IEnumerator MoveToObject(Transform target){yield return null;while (Vector3.Distance(transform.position, target.position) > 0.1f){Vector3 dir = target.position - transform.position;transform.up = dir;transform.position = Vector3.MoveTowards(transform.position, target.position, Time.deltaTime * speed);yield return null;}}private void OnTriggerEnter(Collider other){if(other.gameObject.name=="Target"){Destroy(gameObject);}}
}

第二种:教程发给DeepSeek后的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewBullet : MonoBehaviour
{public float speed = 5f;[SerializeField] private string targetTag = "Target"; // 使用标签检测目标private Transform target;private Coroutine moveCoroutine;private void Start() => Destroy(gameObject, 15f);public void Attack(Vector3 start, Vector3 midPoint, Transform target){this.target = target;moveCoroutine = StartCoroutine(MoveCoroutine(start, midPoint, target));}private IEnumerator MoveCoroutine(Vector3 start, Vector3 midPoint, Transform target){// 贝塞尔曲线阶段Vector3 endPosition = target.position;float duration = CalculateBezierDuration(start, midPoint, endPosition);float t = 0f;while (t < 1f){if (target == null){Destroy(gameObject);yield break;}endPosition = target.position; // 动态更新终点位置UpdatePositionAndRotation(t, start, midPoint, endPosition);t += Time.deltaTime / duration;yield return null;}// 精确追踪阶段while (target != null){UpdatePositionAndRotation(target.position);if (Vector3.Distance(transform.position, target.position) <= 0.1f) break;yield return null;}Destroy(gameObject);}private float CalculateBezierDuration(Vector3 start, Vector3 mid, Vector3 end){// 采样估算曲线长度float sampledLength = 0f;Vector3 prev = start;for (int i = 1; i <= 5; i++){Vector3 point = CalculateBezierPoint(i / 5f, start, mid, end);sampledLength += Vector3.Distance(prev, point);prev = point;}return sampledLength / speed;}private Vector3 CalculateBezierPoint(float t, Vector3 start, Vector3 mid, Vector3 end) =>(1 - t) * (1 - t) * start + 2 * (1 - t) * t * mid + t * t * end;private void UpdatePositionAndRotation(float t, Vector3 start, Vector3 mid, Vector3 end){Vector3 targetPos = CalculateBezierPoint(t, start, mid, end);Vector3 direction = targetPos - transform.position;transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);if (direction != Vector3.zero)transform.up = direction;}private void UpdatePositionAndRotation(Vector3 targetPos){Vector3 direction = targetPos - transform.position;transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);if (direction != Vector3.zero)transform.up = direction;}private void OnTriggerEnter(Collider other){if (other.CompareTag(targetTag))Destroy(gameObject);}private void OnDestroy(){if (moveCoroutine != null)StopCoroutine(moveCoroutine);}
}

相关文章:

unity曲线射击

b站教程 using UnityEngine; using System.Collections;public class BallLauncher : MonoBehaviour {public float m_R;public NewBullet m_BulletPre;public Transform m_Target;private void Start(){StartCoroutine(Attack());}private void OnDestroy(){StopAllCoroutine…...

freecad内部python来源 + pip install 装包

cmake来源&#xff1a; 只能find默认地址&#xff0c;我试过用虚拟的python地址提示缺python3config.cmake 源码来源&#xff1a; pip install 装包&#xff1a; module_to_install "your pakage" import os import FreeCAD import addonmanager_utilities as util…...

【家政平台开发(36)】数据迁移与初始化开发:筑牢家政平台的数据根基

本【家政平台开发】专栏聚焦家政平台从 0 到 1 的全流程打造。从前期需求分析,剖析家政行业现状、挖掘用户需求与梳理功能要点,到系统设计阶段的架构选型、数据库构建,再到开发阶段各模块逐一实现。涵盖移动与 PC 端设计、接口开发及性能优化,测试阶段多维度保障平台质量,…...

Spring Boot 中集成 Knife4j:解决文件上传不显示文件域的问题

Spring Boot 中集成 Knife4j&#xff1a;解决文件上传不显示文件域的问题 在使用 Knife4j 为 Spring Boot 项目生成 API 文档时&#xff0c;开发者可能会遇到文件上传功能不显示文件域的问题。本文将详细介绍如何解决这一问题&#xff0c;并提供完整的解决方案。 Knife4j官网…...

信噪比(SNR)的基本定义

噪比&#xff08;SNR&#xff09;是衡量信号质量的核心指标&#xff0c;定义为有效信号与背景噪声的比值&#xff0c;广泛应用于电子、通信、医学及生物学等领域。 一、定义 基本定义‌ SNR 是信号功率&#xff08;或电压&#xff09;与噪声功率&#xff08;或电压&#xff…...

SpringBoot集成阿里云文档格式转换实现pdf转换word,excel

一、前置条件 1.1 创建accessKey 如何申请&#xff1a;https://help.aliyun.com/zh/ram/user-guide/create-an-accesskey-pair 1.2 开通服务 官方地址&#xff1a;https://docmind.console.aliyun.com/doc-overview 未开通服务时需要点击开通按钮&#xff0c;然后才能调用…...

STM32 模块化开发指南 · 第 5 篇 STM32 项目中断处理机制最佳实践:ISR、回调与事件通知

本文是《STM32 模块化开发实战指南》第 5 篇,聚焦于 STM32 裸机开发中最核心也最容易被忽视的部分——中断服务机制。我们将介绍如何正确、高效地设计中断处理函数(ISR),实现数据与事件从中断上下文传递到主逻辑的通道,并构建一个清晰、可维护、非阻塞的事件通知机制。 一…...

解析Java根基:Object类核心方法

Object类常见方法解析 在Java编程中&#xff0c;Object类是所有类的根类&#xff0c;它包含了许多实用的方法&#xff0c;这些方法在不同的场景下发挥着重要作用。下面我们来详细了解一下Object类中的一些常见方法。 1. toString方法 toString方法是用于将对象转换为字符串表…...

LabVIEW 中 JSON 数据与簇的转换

在 LabVIEW 编程中&#xff0c;数据格式的处理与转换是极为关键的环节。其中&#xff0c;将数据在 JSON 格式与 LabVIEW 的簇结构之间进行转换是一项常见且重要的操作。这里展示的程序片段就涉及到这一关键功能&#xff0c;以下将详细介绍。 一、JSON 数据与簇的转换功能 &am…...

K8s常用基础管理命令(一)

基础管理命令 基础命令kubectl get命令kubectl create命令kubectl apply命令kubectl delete命令kubectl describe命令kubectl explain命令kubectl run命令kubectl cp命令kubectl edit命令kubectl logs命令kubectl exec命令kubectl port-forward命令kubectl patch命令 集群管理命…...

每日算法-250411

这是我今天的 LeetCode 刷题记录和心得&#xff0c;主要涉及了二分查找的应用。 3143. 正方形中的最多点数 题目简述: 思路 本题的核心思路是 二分查找。 解题过程 为什么可以二分&#xff1f; 我们可以对正方形的半边长 len 进行二分。当正方形的半边长 len 越大时&…...

NO.90十六届蓝桥杯备战|动态规划-区间DP|回文字串|Treats for the Cows|石子合并|248(C++)

区间dp也是线性dp的⼀种&#xff0c;它⽤区间的左右端点来描述状态&#xff0c;通过⼩区间的解来推导出⼤区间的解。因此&#xff0c;区间DP的核⼼思想是将⼤区间划分为⼩区间&#xff0c;它的状态转移⽅程通常依赖于区间的划分点。 常⽤的划分点的⽅式有两个&#xff1a; 基于…...

【大模型LLM第十六篇】Agent学习之浅谈Agent loop的几种常见范式

anthropics agent https://zhuanlan.zhihu.com/p/32454721762 code&#xff1a;https://github.com/anthropics/anthropic-quickstarts/blob/main/computer-use-demo/computer_use_demo/loop.py sampling_loop函数 每次进行循环&#xff0c;输出extract tool_use&#xff0…...

数列分块入门4

题目描述 给出一个长为 n n n 的数列&#xff0c;以及 n n n 个操作&#xff0c;操作涉及区间加法&#xff0c;区间求和。 输入格式 第一行输入一个数字 n n n。 第二行输入 n n n 个数字&#xff0c;第 i 个数字为 a i a_i ai​&#xff0c;以空格隔开。 接下来输入…...

学术分享:基于 ARCADE 数据集评估 Grounding DINO、YOLO 和 DINO 在血管狭窄检测中的效果

一、引言 冠状动脉疾病&#xff08;CAD&#xff09;作为全球主要死亡原因之一&#xff0c;其早期准确检测对有效治疗至关重要。X 射线冠状动脉造影&#xff08;XCA&#xff09;虽然是诊断 CAD 的金标准&#xff0c;但这些图像的人工解读不仅耗时&#xff0c;还易受观察者间差异…...

程序化广告行业(77/89):融资、并购与上市全景洞察

程序化广告行业&#xff08;77/89&#xff09;&#xff1a;融资、并购与上市全景洞察 大家好呀&#xff01;一直以来&#xff0c;我都希望能和大家一起在技术知识的海洋里畅游、学习进步。前面我们已经了解了程序化广告行业的发展态势、PC端和移动端投放差异以及行业融资的大致…...

2025年慕尼黑上海电子展前瞻

年岁之约&#xff0c;齐聚慕展&#xff1b; 乘风而起&#xff0c;畅联未来。 2025 年 4 月 15 - 17 日&#xff0c;备受瞩目的慕尼黑上海电子展即将在上海新国际博览中心盛大启幕。回首2024年展会的场景&#xff0c;那热烈非凡的氛围、精彩纷呈的展示仍历历在目&#xff0c;也…...

第十九:b+树和b-树

优点一&#xff1a; B树只有叶节点存放数据&#xff0c;其余节点用来索引&#xff0c;而B-树是每个索引节点都会有Data域。 优点二&#xff1a; B树所有的Data域在叶子节点&#xff0c;并且所有叶子节点之间都有一个链指针。 这样遍历叶子节点就能获得全部数据&#xff0c;这样…...

前沿科技:社会性交互技术原理与核心概念解析

社会性交互中的**情感识别(Emotion Recognition)与拟人化行为生成(Human-like Behavior Generation)**是构建自然、可信人机交互的核心技术,尤其在虚拟助手、社交机器人、元宇宙角色等场景中至关重要。以下是其技术原理、核心方法与实际应用的系统解析: 一、情感识别:从…...

深入浅出Redis 缓存使用问题 | 长文分享

目录 数据一致性 先更新缓存&#xff0c;后更新数据库【一般不考虑】 先更新数据库&#xff0c;再更新缓存【一般不考虑】 先删除缓存&#xff0c;后更新数据库 先更新数据库&#xff0c;后删除缓存【推荐】 怎么选择这些方案&#xff1f;采用哪种合适&#xff1f; 缓存…...

操作系统 3.6-内存换出

换出算法总览 页面置换算法 FIFO&#xff08;先进先出&#xff09;&#xff1a; 最简单的页面置换算法&#xff0c;淘汰最早进入内存的页面。 优点&#xff1a;实现简单。 缺点&#xff1a;可能会导致Belady异常&#xff0c;即增加内存反而降低性能。如果刚换入的页面马上又要…...

【Amazon EC2】为何基于浏览器的EC2 Instance Connect 客户端连接不上EC2实例

文章目录 前言&#x1f4d6;一、报错先知❌二、问题复现&#x1f62f;三、解决办法&#x1f3b2;四、验证结果&#x1f44d;五、参考链接&#x1f517; 前言&#x1f4d6; 这篇文章将讲述我在 Amazon EC2 上使用 RHEL9 AMI 时无法连接到 EC2 实例时所遇到的麻烦&#x1f616; …...

Java并发编程:深入解析原子操作类与CAS原理

一、原子操作类概述 Java并发包(java.util.concurrent.atomic)提供了一系列原子操作类&#xff0c;这些类通过无锁算法实现了线程安全的操作&#xff0c;相比传统的锁机制具有更高的性能。原子类基于CAS(Compare-And-Swap)指令实现&#xff0c;是现代并发编程的重要基础。 原…...

新一代AI低代码MES,助力企业数字化升级

随着DeepSeek低成本AI模型的火热&#xff0c;对于传统的MES而言&#xff0c;在这场AI的盛宴中&#xff0c;该如何去调整产品的定位&#xff0c;让MES更符合工业企业的需求呢&#xff1f; 工业互联网、AI、数字孪生等技术加速与MES融合&#xff0c;实现生产全流程的实时监控与智…...

位运算与实战场景分析-Java代码版

一、为什么每个程序员都要掌握位运算&#xff1f; 在电商秒杀系统中&#xff0c;位运算可以快速判断库存状态&#xff1b;在权限管理系统里&#xff0c;位运算能用极小的空间存储复杂权限配置&#xff1b;在算法竞赛中&#xff0c;位运算更是高频出现的性能优化利器。这项看似…...

面试之《前端信息加密》

前端代码是直接暴漏给用户的&#xff0c;请求的接口也可以通过控制台network看到参数&#xff0c;这是不够安全的&#xff0c;如果遇到坏人想要破坏&#xff0c;可以直接修改参数&#xff0c;或者频繁访问导致系统崩溃&#xff0c;或数据毁坏。 所以信息加密在某些场合就变得非…...

CentOS 系统磁盘扩容并挂载到根目录(/)的详细步骤

在使用 CentOS 系统时&#xff0c;经常会遇到需要扩展磁盘空间的情况。例如&#xff0c;当虚拟机的磁盘空间不足时&#xff0c;可以通过增加磁盘容量并将其挂载到根目录&#xff08;/&#xff09;来解决。以下是一个完整的操作流程&#xff0c;详细介绍了如何将新增的 10G 磁盘…...

HTML应用指南:利用GET请求获取全国汉堡王门店位置信息

在当今快节奏的都市生活中&#xff0c;餐饮品牌的门店布局不仅反映了其市场策略&#xff0c;更折射出消费者对便捷、品质和品牌认同的追求。汉堡王&#xff08;Burger King&#xff09;作为全球知名的西式快餐品牌之一&#xff0c;在中国市场同样占据重要地位。自进入中国市场以…...

浅入浅出 GRPO in DeepSeekMath

GRPO in DeepSeekMath GRPO 通过在生成组内进行比较来直接评估模型生成的响应&#xff0c;以优化策略模型&#xff0c;而不是训练单独的价值模型&#xff0c;这种方法显著降低了计算成本。GRPO 可以应用于任何可以确定响应正确性的可验证任务。例如&#xff0c;在数学推理中&a…...

计算机网络起源

互联网的起源和发展是一个充满创新、突破和变革的历程&#xff0c;从20世纪60年代到1989年&#xff0c;这段时期为互联网的诞生和普及奠定了坚实的基础。让我们详细回顾这一段激动人心的历史。 计算机的发展与ARPANET的建立&#xff08;20世纪60年代&#xff09; 互联网的诞生…...