#NFE Physics Spawning
1 messages · Page 1 of 1 (latest)
The spawning system:
using UnityEngine;
using Unity.Entities;
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using Random = Unity.Mathematics.Random;
public struct Config : IComponentData
{
public Entity CubePrefab;
public int CubeCount;
}
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct CubeSpawningSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
Debug.Log("on_create");
state.RequireForUpdate<Config>();
// state.RequireForUpdate<PhysicsStep>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var config = SystemAPI.GetSingleton<Config>();
var rand = new Random(132131541);
var ecb = new EntityCommandBuffer(Allocator.Temp);
for (int i = 0; i < config.CubeCount; i++)
{
var entity = ecb.Instantiate(config.CubePrefab);
ecb.SetComponent(entity, new LocalTransform
{
Position = new float3(i - 500, 100, 0),
Rotation = quaternion.EulerXYZ(new float3(rand.NextFloat(0, 360), rand.NextFloat(0, 360),
rand.NextFloat(0, 360))),
Scale = 1
});
}
ecb.Playback(state.EntityManager);
// }
state.Enabled = false;
}
}```
The entity prefab
The spawned entities have a "Static" tag, and no PhysicsVelocity components are added
is the cube authoring component setting the TransformFlag to Dynamic?
{
public class Baker : Baker<CubeAuthoring>
{
public override void Bake(CubeAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity,new PhysicsVelocity
{
});
}
}
}```
Following your suggestion I added this CubeAuthoring. However, it's still marked Static, and PhysicsVelocity does not get updated by gravity.
However, if I manually set the PhysicsVelocity values in the editor, they start to move the transforrm
drop a NetCodePhysicsConfig component in your subscene. Also make sure at least one predicted ghost is in the scene.
https://docs.unity3d.com/Packages/com.unity.netcode@1.5/manual/physics.html
I already have a NetCodePhysicsConfig set to "AlwaysRun" for the PhysicsGroup run mode. So there shouldn't need to be a predicted ghost in the scene.
I think its currently a bug. the docs specifically states that at least one predicted ghost needs to exist
I manually added a PhysicsMass and a PhysicsVelocity component to the cubes, and they seem to get updated by physics now.
But they're still not synchronized with the client, and it looks like I needed to set a "NetworkStreamInGame" component on a NetworkId on the server to start synchronizing, which I did, but still doesn't work. 🤷
Appreciate your help though
The docs say, "A ghost is spawned by instantiating it on the server, as all ghosts on the server are replicated to all clients automatically."
Would you know where I could report this, if it's some sort of bug? I'm not sure how everyone doesn't run into this specific issue when using this package.