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:
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.
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)); } }
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); } }
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!