public void Execute([ReadOnly] ref Translation pos, [ReadOnly] ref Rotation rot, ref Move move) { //射线查询 float3 dirNormal = Math.normalize(Math.mul(rot.Value, new float3(0, 0, 1))); RaycastInput input = new RaycastInput() { Ray = new Ray() { Origin = pos.Value + dirNormal, Direction = dirNormal * 1.1f }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; RaycastHit hit = new RaycastHit(); if (World.CastRay(input, out hit)) { move.isHit = 1; } else { move.isHit = 0; } //检测碰撞,重设目标,移动。 if (move.isHit == 0) { if (Math.distancesq(move.targetPos, pos.Value) < 0.01) { move.targetPos = GetRandomTarget(); rot.Value = quaternion.LookRotation(move.targetPos - pos.Value, new float3(0, 1, 0)); } #region 加速度 if (move.speed < move.maxSpeed) { move.speed += move.acceleratioin * time; } pos.Value += Math.normalize(move.targetPos - pos.Value) * move.speed * time; #endregion } else //碰到障碍,右转180度/秒 { move.speed = 0; //减速 move.targetPos = math.rotate(quaternion.AxisAngle(new float3(0, 1, 0), (float)math.PI * time), (move.targetPos - pos.Value)) + pos.Value; rot.Value = quaternion.LookRotation(move.targetPos - pos.Value, new float3(0, 1, 0)); } }
public static bool Raycast(float3 RayFrom, float3 RayTo, out Unity.Physics.RaycastHit outHit, out Unity.Entities.Entity entity) { var physicsWorldSystem = Unity.Entities.World.Active.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Start = RayFrom, End = RayTo, Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); bool haveHit = collisionWorld.CastRay(input, out hit); outHit = hit; entity = Unity.Entities.Entity.Null; if (haveHit) { // see hit.Position // see hit.SurfaceNormal entity = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(true); } return(false); }
public float3 RaycastHitLocation(float3 RayFrom, float3 Direction) { var physicsWorldSystem = World.Active.GetExistingSystem <BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Ray = new Unity.Physics.Ray() { Origin = RayFrom, Direction = Direction }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { return(hit.Position); } return(float3.zero); }
public void Raycast(float3 origin, float3 direction) { var physicsWorldSystem = World.Active.GetExistingSystem <BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Ray = new Ray() { Origin = origin, Direction = direction }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; RaycastHit hit = new RaycastHit(); if (collisionWorld.CastRay(input, out hit)) { // see hit.Position // see hit.SurfaceNormal // Entity e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; Debug.Log(hit.Position); } }
private Entity Raycast(Vector3 _from, Vector3 _to, int _belongsTo, int _collidesWith) { var physicWorldSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem <BuildPhysicsWorld>(); var collisionWorldSystem = physicWorldSystem.PhysicsWorld.CollisionWorld; var input = new RaycastInput { Start = _from, End = _to, Filter = new CollisionFilter { BelongsTo = (uint)(1 << _belongsTo), CollidesWith = (uint)(1 << _collidesWith), GroupIndex = 0 } }; RaycastHit hit = new RaycastHit(); bool haveHit = collisionWorldSystem.CastRay(input, out hit); if (haveHit) { Entity hitEntity = physicWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(hitEntity); } return(Entity.Null); }
public static Entity Raycast(Vector3 mousePosition, out RaycastHit hit, BuildPhysicsWorld buildPhysicsWorldSystem) { UnityEngine.Ray inputRay = Camera.main.ScreenPointToRay(mousePosition); float3 RayFrom = inputRay.origin; float3 RayTo = inputRay.origin + inputRay.direction * 500f; var collisionWorld = buildPhysicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Start = RayFrom, End = RayTo, Filter = CollisionFilter.Default }; bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { Entity e = buildPhysicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(e); } return(Entity.Null); }
public void Execute() { //创建输入 RaycastInput raycastInput = new RaycastInput() { Start = mStartPos, End = mEndPos * 100, //声明碰撞过滤器,用来过滤某些层级下的物体是否进行射线检测 Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0, } }; Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit(); // 发射射线去检测Entity实体 if (physicsWorld.CollisionWorld.CastRay(raycastInput, out raycastHit)) { //拿到我们射线击中的entity Bodies[0] = physicsWorld.Bodies[raycastHit.RigidBodyIndex]; //拿到击中点的位置信息 rayHitpos[0] = raycastHit.Position; } }
/// <summary> /// 射线查询 /// </summary> /// <param name="origin"></param> /// <param name="dir"></param> /// <returns></returns> int RayCast(float3 origin, float3 dir) { RaycastInput input = new RaycastInput() { Ray = new Ray() { Origin = origin, Direction = dir }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; RaycastHit hit = new RaycastHit(); if (World.CastRay(input, out hit)) { return(1); } else { return(0); } }
private Entity Raycast(float3 fromPosition, float3 toPosition) { BuildPhysicsWorld buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem <BuildPhysicsWorld>(); CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld; RaycastInput raycastInput = new RaycastInput { Start = fromPosition, End = toPosition, Filter = new CollisionFilter { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0, } }; Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit(); if (collisionWorld.CastRay(raycastInput, out raycastHit)) { // Hit Entity hitEntity = buildPhysicsWorld.PhysicsWorld.Bodies[raycastHit.RigidBodyIndex].Entity; return(hitEntity); } else { return(Entity.Null); } }
private Entity currentChunk(Camera mainCamera) { var physicsWorldSystem = World.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Start = mainCamera.transform.position, End = new float3(mainCamera.transform.position.x, 0, mainCamera.transform.position.z), Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { // see hit.Position // see hit.SurfaceNormal Entity e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(e); } return(Entity.Null); }
private void MarkSelected(RaycastHit hit) { var selectedEntity = PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; var ballComponent = EntityManager.HasComponent<BugComponent>(selectedEntity) ? EntityManager.GetComponentData<BugComponent>(selectedEntity) : new BugComponent(); EntityManager.AddComponent<ChainMarkComponent>(selectedEntity); EntityManager.SetComponentData(selectedEntity, new ChainMarkComponent { NeededColor = ballComponent.Color}); }
public Entity RaycastEntity(float3 RayFrom, float3 Direction) { var physicsWorldSystem = World.Active.GetExistingSystem <BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Ray = new Unity.Physics.Ray() { Origin = RayFrom, Direction = Direction }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { // see hit.Position // see hit.SurfaceNormal Entity e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(e); } return(Entity.Null); }
private bool Raycast(float3 from, float3 to, out float3 point, out Entity entity) { BuildPhysicsWorld physicsWorld = World.GetOrCreateSystem <BuildPhysicsWorld>(); CollisionWorld collisionWorld = physicsWorld.PhysicsWorld.CollisionWorld; RaycastInput raycastInput = new RaycastInput { Start = from, End = to, Filter = new CollisionFilter { BelongsTo = ~0u, CollidesWith = ~0u, GroupIndex = 0 } }; var hit = new Unity.Physics.RaycastHit(); if (collisionWorld.CastRay(raycastInput, out hit)) { point = hit.Position; entity = hit.Entity; return(true); } point = hit.Position; entity = hit.Entity; return(false); }
protected override void OnUpdate() { var collisionWorld = builtInPhysicsWorld.PhysicsWorld.CollisionWorld; float deltaTime = Time.DeltaTime; Entities .WithoutBurst() .ForEach((ref Speed speed, in Translation translation, in Direction direction, in PhysicsCollider collider) => { if (speed.Value > 0) { RaycastInput input = new RaycastInput() { Start = translation.Value, End = translation.Value + new float3(0.1350121f * direction.Value, 0, 0), Filter = collider.Value.Value.Filter }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); Debug.DrawLine(translation.Value, translation.Value + new float3(0.12f * direction.Value, 0, 0)); if (collisionWorld.CastRay(input, out hit)) { speed.Value = 0; } } }).Run();
// raycast caring about both where and what was hit private bool Raycast(float3 fromPosition, float3 toPosition, ref Unity.Physics.RaycastHit hit, ref Entity e) { BuildPhysicsWorld buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); //Get the build physics world CollisionWorld collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld; //get the collision world RaycastInput raycastInput = new RaycastInput { Start = fromPosition, End = toPosition, Filter = new CollisionFilter { BelongsTo = ~0u, // belongs to all layers CollidesWith = ~0u, // collides with all layers GroupIndex = 0, // a new group } }; if (collisionWorld.CastRay(raycastInput, out hit)) // use the collision world to cast a ray // hit something { e = buildPhysicsWorld.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; return(true); } else { return(false); } }
private float DistanceToGround(Camera mainCamera) { var physicsWorldSystem = World.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; RaycastInput input = new RaycastInput() { Start = mainCamera.transform.position, End = new float3(mainCamera.transform.position.x, 0, mainCamera.transform.position.z), Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { return(math.length(hit.Position - new float3(mainCamera.transform.position))); } return(0f); }
public static bool CastRay(float3 start, float3 end, out Unity.Physics.RaycastHit result, CollisionWorld world, CollisionFilter filter) { var input = new RaycastInput { Start = start, End = end, Filter = filter, }; return(world.CastRay(input, out result)); }
public static void SingleRayCast(CollisionWorld world, RaycastInput input, ref RaycastHit result, JobHandle inputDeps) { NativeArray <RaycastInput> rayCommands = new NativeArray <RaycastInput>(1, Allocator.TempJob); NativeArray <RaycastHit> rayResults = new NativeArray <RaycastHit>(1, Allocator.TempJob); rayCommands[0] = input; var handle = ScheduleBatchRayCast(world, rayCommands, rayResults, inputDeps); handle.Complete(); result = rayResults[0]; rayCommands.Dispose(); rayResults.Dispose(); }
private void MouseHoverFormations() { if (!Input.GetMouseButton(1) && !_isDragging) { Unity.Physics.RaycastHit hit = SingleRaycast(); if (hit.Entity != Entity.Null) { } else { } } }
public static void SingleRayCast(Unity.Physics.CollisionWorld world, Unity.Physics.RaycastInput input, ref Unity.Physics.RaycastHit result) { var rayCommands = new NativeArray <Unity.Physics.RaycastInput>(1, Allocator.TempJob); var rayResults = new NativeArray <Unity.Physics.RaycastHit>(1, Allocator.TempJob); rayCommands[0] = input; var handle = ScheduleBatchRayCast(world, rayCommands, rayResults); handle.Complete(); result = rayResults[0]; rayCommands.Dispose(); rayResults.Dispose(); }
private bool Raycast(float3 rayFrom, float3 rayTo, out RaycastHit raycastHit) { var input = new RaycastInput() { Start = rayFrom, End = rayTo, Filter = new CollisionFilter { BelongsTo = (uint)CollisionLayers.Selection, CollidesWith = (uint)(CollisionLayers.Ground | CollisionLayers.Units) } }; return(_collisionWorld.CastRay(input, out raycastHit)); }
public bool Raycast(float3 rayFrom, float3 rayTo, out RaycastHit hit) { var collisionWorld = m_physicsWorldSystem.PhysicsWorld.CollisionWorld; var input = new RaycastInput() { Start = rayFrom, End = rayTo, Filter = CollisionFilter.Default }; var haveHit = collisionWorld.CastRay(input, out var hitResult); hit = hitResult; return(haveHit); }
public JobHandle SingleRayCast(RaycastInput input, ref RaycastHit result) { var rayCommands = new NativeArray <RaycastInput>(1, Allocator.TempJob); var rayResults = new NativeArray <RaycastHit>(1, Allocator.TempJob); rayCommands[0] = input; var handle = ScheduleBatchRayCast(rayCommands, rayResults); handle.Complete(); result = rayResults[0]; Debug.Log($"collisionPos{result.Position}"); rayCommands.Dispose(); rayResults.Dispose(); handle.Complete(); return(handle); }
public Entity Raycast(float3 RayFrom, float3 RayTo) { var physicsWorldSystem = Unity.Entities.World.DefaultGameObjectInjectionWorld.GetExistingSystem <BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; var input = new RaycastInput() { Start = RayFrom, End = RayTo, Filter = new CollisionFilter() { BelongsTo = 1u << 3, CollidesWith = 1u << 3, GroupIndex = 0 } }; var hit = new RaycastHit(); var haveHit = collisionWorld.CastRay(input, out hit); // if (haveHit) // { // for (int c = 0; c < hits.Length; c++) // { // var hit = hits[c]; // var e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; // EventQueue.Enqueue(new EventBubbleData // { // EventEntity = e, // Hit = hit, // }); // } // } if (haveHit) { // see hit.Position // see hit.SurfaceNormal var e = physicsWorldSystem.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; EventQueue.Enqueue(new EventBubbleData { EventEntity = e, Hit = hit, }); return(e); } return(Entity.Null); }
public void Execute([ReadOnly] ref Translation pos, [ReadOnly] ref Rotation rot, ref Move move) { float3 dirNormal = Math.normalize(Math.mul(rot.Value, new float3(0, 0, 1))); RaycastInput input = new RaycastInput() { Ray = new Ray() { Origin = pos.Value + dirNormal * 0.51f, Direction = dirNormal * 1.1f }, Filter = new CollisionFilter() { CategoryBits = ~0u, // all 1s, so all layers, collide with everything MaskBits = ~0u, GroupIndex = 0 } }; RaycastHit hit = new RaycastHit(); if (World.CastRay(input, out hit)) { // see hit.Position move.isHit = 1; // Debug.Log(hit.Position); } else { move.isHit = 0; } //检测碰撞,重设目标,移动。 if (move.isHit == 0) { if (Math.distancesq(move.targetPos, pos.Value) < 0.01) { move.targetPos = GetRandomTarget(); rot.Value = quaternion.LookRotation(move.targetPos - pos.Value, new float3(0, 1, 0)); } pos.Value += Math.normalize(move.targetPos - pos.Value) * time; } else { move.targetPos = GetRandomTarget(); rot.Value = quaternion.LookRotation(move.targetPos - pos.Value, new float3(0, 1, 0)); } }
private float3 MouseToWorldPosition(Vector2 mousePos, Camera cam) { RaycastInput RaycastInput = new RaycastInput { Start = cam.transform.position, End = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.farClipPlane)), Filter = CollisionFilter.Default }; CollisionWorld collisionWorld = EntityManager.World.GetExistingSystem <BuildPhysicsWorld>().PhysicsWorld.CollisionWorld; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); RayCastJobUtils.SingleRayCast(collisionWorld, RaycastInput, ref hit); return(hit.Fraction == 0 ? RaycastInput.End : hit.Position); }
public void OnMouseLeftButton(InputAction.CallbackContext context) { BuildPhysicsWorld stepWorld = World.GetOrCreateSystem <BuildPhysicsWorld>(); CollisionWorld collisionWorld = stepWorld.PhysicsWorld.CollisionWorld; Ray ray = Camera.main.ScreenPointToRay(mousePosition); RaycastInput raycastInput = new RaycastInput { Start = ray.origin, End = ray.origin + ray.direction * 1000f, Filter = CollisionFilter.Default, }; RaycastHit closestHit = new RaycastHit(); if (collisionWorld.CastRay(raycastInput, out closestHit)) { Bang(closestHit.Position); } }
private Unity.Physics.RaycastHit SingleRaycast() { UnityEngine.Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastInput raycastInput = new RaycastInput { Start = ray.origin, End = ray.GetPoint(2000f), Filter = new CollisionFilter() { BelongsTo = ~0u, CollidesWith = ~0u, // all 1s, so all layers, collide with everything GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); RaycastUtils.SingleRayCast(_world, raycastInput, ref hit, this.Dependency); return(hit); }
protected override void OnUpdate() { Entities .WithoutBurst() .ForEach((Entity entity, ref Attack attack, ref AlreadyAttackData alreadyAttack, in Translation translation, in Direction direction, in PhysicsCollider collider) => { var collisionWorld = builtInPhysicsWorld.PhysicsWorld.CollisionWorld; if ((attack.attack > 0 || EntityManager.GetName(entity) == "Boss") && !alreadyAttack.Value) { RaycastInput input = new RaycastInput() { Start = translation.Value + new float3(attack.attackMargin * direction.Value, 0, 0), End = translation.Value + new float3(attack.attackDistance * direction.Value, 0, 0), Filter = collider.Value.Value.Filter }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); Debug.DrawLine(translation.Value + new float3(attack.attackMargin * direction.Value, 0, 0), translation.Value + new float3(attack.attackDistance * direction.Value, 0, 0)); if (collisionWorld.CastRay(input, out hit)) { Entity hitEntity = builtInPhysicsWorld.PhysicsWorld.Bodies[hit.RigidBodyIndex].Entity; if (EntityManager.HasComponent <TakeDamage>(hitEntity) && !EntityManager.GetComponentData <TakeDamage>(hitEntity).alreadyTakeDamage&& attack.attack > 0) { EntityManager.SetComponentData(hitEntity, new TakeDamage { takeDamage = true, damage = attack.attackDamage, attackDirection = direction.Value }); alreadyAttack.Value = true; } if (EntityManager.HasComponent <TakeDamage>(hitEntity) && attack.attack == 0) { Debug.Log(hitEntity.Index); EntityManager.SetComponentData(entity, new StrikeData { Value = true }); } } else if (attack.attack == 0) { EntityManager.SetComponentData(entity, new StrikeData { Value = false }); } } }).Run();
protected override JobHandle OnUpdate(JobHandle inputDeps) { var physicsWorldSystem = Unity.Entities.World.Active.GetExistingSystem <Unity.Physics.Systems.BuildPhysicsWorld>(); var collisionWorld = physicsWorldSystem.PhysicsWorld.CollisionWorld; var mousePostion = Input.mousePosition; var unityRay = Camera.main.ScreenPointToRay(mousePostion); var ray = new Unity.Physics.Ray(unityRay.origin, unityRay.direction * 10000); Unity.Physics.RaycastInput input = new Unity.Physics.RaycastInput() { Ray = ray, Filter = new CollisionFilter() { CategoryBits = ~0u, MaskBits = ~0u, GroupIndex = 0 } }; Unity.Physics.RaycastHit hit = new Unity.Physics.RaycastHit(); SingleRayCast(collisionWorld, input, ref hit); bool haveHit = collisionWorld.CastRay(input, out hit); if (haveHit) { mousePostion = new float3(hit.Position.x, hit.Position.y, hit.Position.z); } //Debug.Log("Mouse Positon: " + mousePostion); var job = new MouseInputJob { leftClick = Input.GetMouseButtonDown(0), rightClick = Input.GetMouseButtonDown(1), mousePosition = mousePostion }; return(job.Schedule(this, inputDeps)); }