Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Unity Physicsの使用例
using Unity.Entities;
using Unity.Physics.Systems;
[UpdateAfter(typeof(StepPhysicsWorld))]
public class ItemGetSystem: ComponentSystem
{
private BuildPhysicsWorld buildPhysicsWorldSystem;
private StepPhysicsWorld stepPhysicsWorldSystem;
protected override void OnCreate()
{
buildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
stepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
}
protected override void OnUpdate()
{
var physicsWorld = buildPhysicsWorldSystem.PhysicsWorld;
var triggerEvents = stepPhysicsWorldSystem.Simulation.TriggerEvents;
var items = GetComponentDataFromEntity<ItemTag>(true);
foreach (var triggerEvent in triggerEvents)
{
// PhysicsWorldに全ての物理演算で動くEntityが含まれる。
// triggerEventsには、Raises Collision Eventが有効なオブジェクトが接触イベントを格納する
// TriggerEventの中身を洗えば、接触判定が取れる
var bodyA = physicsWorld.Bodies[triggerEvent.BodyIndices.BodyAIndex];
var bodyB = physicsWorld.Bodies[triggerEvent.BodyIndices.BodyBIndex];
Process(bodyA.Entity, items);
Process(bodyB.Entity, items);
}
}
void Process(Entity entity, ComponentDataFromEntity<ItemTag> items)
{
if( items.Exists(entity))
{
PostUpdateCommands.DestroyEntity(entity);
UnityEngine.Debug.Log("you get item!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.