Created
April 25, 2019 13:30
-
-
Save tsubaki/80a4083ca1a524c168be8d6bdabdad1c to your computer and use it in GitHub Desktop.
Unity Physicsの使用例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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