原因
在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)
{
}
}
}
设置后