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

unity的dots中instantiate克隆对象后,对象会在原位置闪现的原因和解决

原因

在Entity中有两个位置信息,一个是local transform。一个是local to world
在这里插入图片描述
其中local transform负责具体位置,local to world 负责渲染位置,即图像的渲染的位置是根据local to world的。
local to world 的更新是引擎自己控制的,自动匹配local transform,每帧会触发一次,在local to world system中完成
在这里插入图片描述
你的system中如果包含克隆和修改local transform,且发生在local to world system之后。
当你的system完成后,屏幕中就会立刻出现这个物体他的local transform已经改变,但local toworld依旧是预制体的位置,因此会在原地渲染一帧。

具体如图:

第一帧
在这里插入图片描述
图中红色圈为子弹出现的位置,橙色圈是子弹应出现的位置。
下一帧
在这里插入图片描述
可以看到在下一帧立刻恢复了正常
第一帧
在这里插入图片描述
可以看到local to world 与local transform不同
下一帧
在这里插入图片描述
local to world 在下一帧自动匹配上了local transform,子弹的位置也发生了改变。

systems具体情况
在这里插入图片描述
其中localtoworld system负责改变local to world 的值,我自己的shoot attack system负责克隆对象和一些初始化的操作。
这一帧里local to world 执行完成后才执行了shoot attack system,因此这一帧里子弹会在原位置渲染一帧
下一帧调用过local to world后,才会恢复正常。

解决方案

我们注意到local to world 属于transform system group。而transform system group 和shoot attack system都属于simulation,那么就可以这样设置。

[UpdateInGroup(typeof(SimulationSystemGroup))]
[UpdateBefore(typeof(TransformSystemGroup))]
public partial struct ShootAttackSystem : ISystem
{

具体如下

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;

namespace Script.Systems
{
    [UpdateInGroup(typeof(SimulationSystemGroup))]
    [UpdateBefore(typeof(TransformSystemGroup))]
    public partial struct ShootAttackSystem : ISystem
    {
        
        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            
        }
    }
}

设置后
在这里插入图片描述

结果

请添加图片描述

相关文章:

  • 吉卜力动画风格图像生成:Ghibli Diffusion
  • SDL中SDL_AudioSpec结构体参数
  • 23 MVC模式
  • Android学习总结之网络篇(HTTP请求流程)
  • Day82 | 灵神 | 快慢指针 重排链表
  • 科技赋能消防:无人机“挂弹灭火“构筑森林防火墙!
  • 生成式人工智能认证(GAI认证)如何推动就业市场的创新?
  • 【橘子大模型】关于记忆上下文Message History
  • Jenkins学习(B站教程)
  • [从零开始学习JAVA] 初学网络编程
  • 设计模式简述(十二)策略模式
  • [GN] sigrokdecode 模块学习指南 --- 准备阶段
  • COZE通关指南:工作流与插件开发
  • Go语言-初学者日记(四):包管理
  • 分组(二分查找)
  • GORM-Golang的ORM框架(1)
  • 2000-2019年 全国各省份GDP、人口、城镇化率统计数据
  • 深度研究deep-research优秀开源
  • Windows环境下开发pyspark程序
  • Flask + Pear Admin Layui 快速开发管理后台
  • php 开发动态网站开发/搜索引擎最佳化
  • 四川建设行政主管部门官方网站/百度的搜索引擎优化
  • 品牌网站建设哪里有/网页设计框架图
  • 最好记得网站域名/河北关键词seo排名
  • 网络舆情工作总结报告/优化疫情防控
  • 保定网站设计公司/国外搜索引擎大全百鸣