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)
        {
            
        }
    }
}

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

结果

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值