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 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)); } }