1

So I'm making a game like Splash Canyons (link), where I've to detect the collision point of the invisible blade (which follows the finger position) and the board. I'm able to do that when I slice slowly, but as I slice fast, unity isn't able to detect the collision of the finger and the board. How can I do that?

Approaches I've tried:

  1. Rigidbody collisions: The blade is a small sphere without mesh renderer but with a non-kinematic rigidbody (interpolate = none and collision detection = continuous), and a sphere collider with isTrigger set as enabled. The object to detect collisions with (the board) has box collider with isTrigger unchecked and its parent has a rigidbody with interpolate as none and collision detection as Discrete (I've tried many ways toggling this though). Rigidbody is attached to the parent of that collided object because the parent object also has a Hinge Joint component, for which Rigidbody is a must.

  2. Raycasting: I've tried this also but same, no progress with the speed part. Also, one more problem that arises in raycasting, is that when we cut horizontally along the length of the board, it registers many cuts which I don't want ofcourse. Still, I'm attaching the code just in case anyone can notice if I'm missing out on something:

     RaycastHit hit = new RaycastHit();
     Vector3 screenPoint = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, from));
     Ray ray = new Ray(screenPoint, new Vector3(screenPoint.x, screenPoint.y, to));
     Debug.DrawRay(screenPoint, new Vector3(screenPoint.x, screenPoint.y, to));
     if (Physics.Raycast(ray, out hit, 1000)) {
         Debug.Log(hit.transform.name);
         if (hit.transform.tag == "Plank") {
             hit.transform.GetChild(0).GetComponent<Plank>().RegisterCut(new Vector3(hit.point.x, hit.point.y, 0.0f));
         }
     }
    
  3. Physics.OverlapSphere: Also tried this approach but still facing the fast swipe problem with this also. Also more problems with this. Code:-

     foreach (Collider c in Physics.OverlapSphere(transform.position, 0.1f)) {
         if (c.transform.parent.tag == "Plank") {
             c.GetComponent<Plank>().RegisterCut(transform.position);
         }
     }
    
  4. Tried changing the time settings in the Project settings but that makes many things even worse, regarding the collisions only. These are the approaches I've tried till now. It'd be really great if someone could help me get this solved by the Rigidbody approach only.

Thanks!

  • have you tried collision detection = Continuous Dynamic? – Art Zolina III Apr 10 '21 at 17:45
  • @ArtZolinaIII yes, I've tried that also. – Devansh Sharma Apr 10 '21 at 17:55
  • have you tried Rigidbody2D.sleepMode = never sleep? – Art Zolina III Apr 10 '21 at 18:09
  • What registercut() is doing? Can you share the code – Syed Mohib Uddin Apr 10 '21 at 20:26
  • I would check out [this thread](https://stackoverflow.com/questions/9688237/how-to-prevent-colliders-from-passing-through-each-other). It is using a raycast similar to how an implementation of sweeping collision works by checking ahead of the object in smaller stepped increments. To fix the horizontal issue, just mark something as cut once it is cut. Then to ignore it, change the layer that the cut planks are on, then ignore that layer in the cast. Raycasting should work and the layer change should fix your multi horizontal issue. I can type up an example if you need. – TEEBQNE Apr 11 '21 at 03:48
  • @ArtZolinaIII I'm not using 2D rigidbody, so can't do this. – Devansh Sharma Apr 11 '21 at 06:34
  • @SyedMohibUddin register cut is just a function to divide the plank into 2 pieces :D – Devansh Sharma Apr 11 '21 at 06:35
  • @TEEBQNE Got it, raycast also works fine for me but not at a greater speed. I mean when I swipe instantly. And for the horizontal issue, can't mark anything as cut once cut because I want to perform further cutting also. But anyway, that can be fixed. The main problem is detecting cuts at a higher speed which isn't yet possible through raycasting as well :( Let me know what you think about this.. – Devansh Sharma Apr 11 '21 at 06:39
  • @DevanshSharma I know what it does want t to check how it do it. – Syed Mohib Uddin Apr 11 '21 at 08:00
  • try using linecast where the start point is the first touch and the end of the line is the touch drag up. – Art Zolina III Apr 11 '21 at 08:25

0 Answers0