ã©ãããåéã§ãã
UnityECSã§ã®ç©çã·ãã¥ã¬ã¼ã·ã§ã³ã§ã¯Physicsã¨ããã©ã¤ãã©ãªã使ç¨ããã¾ãã
以ä¸ãã¤ã³ãã¼ã - com.unity.physics
ãã®Physicsã使ç¨ãã¦ãã¬ã¤ã¤ã¼ã«ä¸çªè¿ããªãã¸ã§ã¯ãåå¾ãå¾æ¥ã®Physicsã®ããã«è¡ãããã¨æã£ãã®ã§ãããã°ã°ãã¨
_buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>(); _stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
ãªã©ã®ãããªè¨è¿°é¨åããã£ãã®ã§ãããææ°ç(Unity6)ã§ã¯BuildPhysicsWorldã¯ãã®ããã«åå¾ã§ããªãã§ãããStepPhysicsWorldã¯ããããåå¨ãã¦ãã¾ããã§ããã
ããã§èªåã§ãããã調ã¹ã¦ããã®ã§ããã以ä¸ã®æãã§ã§ããããªãã§ãããã¾ã ãã¾ããã£ã¦ãã¾ããã
void Execute(ref CitizenBase citizen, ref LocalTransform transform, ref PhysicsWorld physicsWorld) { float3 currentPosition = transform.Position; // æ¢ç´¢ç¯å²ã®è¨å® float searchRadius = 10f; var distanceHits = new NativeList<DistanceHit>(); var collisionWorld = physicsWorld.CollisionWorld; // Sphereãã£ã¹ãã§æãè¿ããªãã¸ã§ã¯ããæ¢ã collisionWorld.OverlapSphere(currentPosition, searchRadius, ref distanceHits, CollisionFilter.Default); var closestHit = GetClosestHit(distanceHits, currentPosition); Debug.Log(closestHit.Entity); } static DistanceHit GetClosestHit(NativeList<DistanceHit> hits, float3 userPosition) { DistanceHit closestHit = default; float closestDistance = float.MaxValue; foreach (var hit in hits) { float distance = math.distance(userPosition, hit.Position); // è·é¢è¨ç® if (distance < closestDistance) { closestDistance = distance; closestHit = hit; } } return closestHit; }
ã¾ã 調æ»ä¸ã