CollisionDetector improves collision detections in your Unity project.
Delegate version and UniRx version are available.
Collision detection on the vibrated ground is unstable as the following movie.
CollisionDetector makes the collision detection more stable as the following movie.
Attach CollisionDetector script to a GameObject you want to detect the smoothed collision using AddComponent. Settings of CollisionDetector are as follows.
Check tag of collided GameObject or not.
if Check Other Tag is true, set the tag you want to detect.
Stability factor of the smoothed collision detection. Lower cut-off frequency makes the smoothed collision detection more slowly.
Threshold of the smoothed collision detection. Difference between the upper and the lower threshold is a hysteresis. The larger hysteresis makes the smoothed detection more stable.
In the script you want to use, declare as follows.
using NnanaSoft.CollisionDetector;
And use GetComponent() in Start or Awake method.
The next step is different between Delegate version and UniRx version.
Add the method to the delegate as follows.
private void Start()
}
var SmoothedCollision = GetComponent<CollisionDetector>();
// OnCollisionEnter
SmoothedCollision.onCollisionEnterSmoothed += OnCollisionEnterMethod;
// OnCollisionStay
SmoothedCollision.onCollisionStaySmoothed += OnCollisionStayMethod;
// OnCollisionExit
SmoothedCollision.OnCollisionEnterSmoothed += OnCollisionExitMethod;
}
// OnCollisionEnter
private void OnCollisionEnterMethod(Collision collision)
{
// Write a method you want to do
Debug.Log("OnCollisionEnterSmoothed");
}
// OnCollisionStay
private void OnCollisionStayMethod(Collision collision)
{
// Write a method you want to do
Debug.Log("OnCollisionStaySmoothed");
}
// OnCollisionEnter
private void OnCollisionExitMethod(Collision collision)
{
// Write a method you want to do
Debug.Log("OnCollisionExitSmoothed");
}
Import UniRx to your Unity project and write using directive as follows.
using UniRx;
using UniRx.Triggers;
Add the method in Start or Awake method as follows
private void Start()
}
var SmoothedCollision = GetComponent<CollisionDetector>();
// OnCollisionEnter
SmoothedCollision.OnCollisionEnterSmoothedAsObservable
.Subscribe(other =>
{
// Write a method you want to do
Debug.Log("OnCollisionEnterSmoothed");
});
// OnCollisionStay
SmoothedCollision.OnCollisionStaySmoothedAsObservable
.Subscribe(other =>
{
// Write a method you want to do
Debug.Log("OnCollisionStaySmoothed");
});
// OnCollisionExit
SmoothedCollision.OnCollisionEnterSmoothedAsObservable
.Subscribe(other =>
{
// Write a method you want to do
Debug.Log("OnCollisionExitSmoothed");
});
}