Function for hit detection