- Home /
Cannot move anything with pure ECS Physics instantiation
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[i];
// 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();
}
}
I have the same problem. Did you ever find a solution?
Answer by wiiiteekk · Oct 04, 2020 at 07:19 AM
I found a solution. To make a dynamic body you have to set it mass properly. And to set mass you have to create collider first. This is how I am adding rigid body dynamically
Entities.WithAll<EnableSimulation_TAG>().ForEach((Entity e, int entityInQueryIndex, in Translation translation, in PhysicsCollider collider) => {
// the most important part here:
PhysicsMass pm = PhysicsMass.CreateDynamic(collider.MassProperties, 1.2f);
ecb.AddComponent<PhysicsMass>(entityInQueryIndex, e, pm);
ecb.AddComponent<PhysicsVelocity>(entityInQueryIndex, e, new PhysicsVelocity {
Linear = new float3(0.00f, 0, 0)
});
ecb.AddComponent<PhysicsDamping>(entityInQueryIndex, e, new PhysicsDamping {
Linear = 0.0f
});
ecb.RemoveComponent<EnableSimulation_TAG>(entityInQueryIndex, e);
}).ScheduleParallel();
Your answer
![](https://koobas.hobune.stream/wayback/20220612232552im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Physics Unity Classic vs Physics Unity Dots 1 Answer
Dots physics ComputePenetration equivalent 1 Answer
DOTS trigger event is firing twice? 0 Answers
Unity (ECS & DOTS) Entity Sinking 0 Answers
Unity DOTS Physics constraint linear and angular x,y,z 0 Answers