I am messing around with unity’s new physics and learning ECS at the same time. My hope was to spawn a whole lot of spheres via ECS and have physics move them (as in initially just respond to gravity). Everything I try thus far does nothing.
I can add a normal gameobject sphere, attach a physics body, physics shape, and convert to entity script in same scene and that one drops like it should. I’ve mirrored all the components I found in the entity debugger from the gameobject sphere version in the pure ECS ones and they still don’t move.
Does anyone have example code somewhere or can spot whats wrong with my simple script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Transforms;
using Unity.Rendering;
using Unity.Collections;
using Unity.Physics;
using Unity.Mathematics;
public class GameManager : MonoBehaviour
{
[SerializeField]
UnityEngine.Material mat;
[SerializeField]
Mesh mesh;
// Start is called before the first frame update
void Start()
{
EntityManager em = World.DefaultGameObjectInjectionWorld.EntityManager;
EntityArchetype eArch = em.CreateArchetype(
typeof(Translation), // gives it a transform
typeof(RenderMesh), // gives it a renderer
typeof(RenderBounds), // needed with render mesh
typeof(LocalToWorld), // gives it world coordinates
typeof(PhysicsMass), // add mass to the objects
typeof(PhysicsDamping), // not sure
typeof(PhysicsVelocity),// allow moving
typeof(PhysicsCollider),// bang into stuffs
typeof(Ball));
NativeArray<Entity> eArr = new NativeArray<Entity>(3000, Allocator.Temp);
em.CreateEntity(eArch, eArr);
for(int i=0; i < eArr.Length; i++)
{
Entity e = eArr*;*
// my dummy component
em.SetComponentData(e, new Ball { damage = 10 });
// spawn randomly somewhere
em.SetComponentData(e, new Translation
{
Value = new Unity.Mathematics.float3(UnityEngine.Random.Range(-50, 50),
UnityEngine.Random.Range(-100, 0),
UnityEngine.Random.Range(-50, 50))
});
// init a dynamic mass, then set the values that apparently dont get intialized.
PhysicsMass pm = PhysicsMass.CreateDynamic(new Unity.Physics.MassProperties(), 10);
pm.InverseInertia = new float3(6f, 6f, 6f);
pm.InverseMass = 1.0f;
em.SetComponentData(e,pm);
// little dampening
em.SetComponentData(e, new PhysicsDamping
{
Angular = 0.05f,
Linear = 0.01f
});
// not sure if this needs any initializing, but exists on GO sphere version.
em.SetComponentData(e, new PhysicsCollider
{
});
// initial velocity to resist gravity some?
em.SetComponentData(e, new PhysicsVelocity
{
Angular = new float3(0.0f, 0.0f, 0.0f),
Linear = new float3(0.0f, 10.0f, 0.0f)
});
// setup rendermesh
em.SetSharedComponentData(e, new RenderMesh
{
material = mat,
mesh = mesh
});
}
eArr.Dispose();
}
}