Implement particle scattering after collision for a Collider/Rigidbody/Trigger GameObject
I am using Unity to simulate the motion of particles confined within a box under the influences of gravity and particle-particle and particle-box collisions (The box is made of 6 cubes, which are static colliders). I want to achieve the functionality that, during the animation when a user clicks a certain particle, its velocity will be displayed. To implement this, I made each particle a Collider/Rigidbody/Trigger GameObject. Trigger is intended for using the OnMouseDown function. However, as the built-in physics engine does not apply to a Trigger, I am stuck in implementing the physics of scattering of the particle after collision, specifically in obtaining the normal vector of the contact surface. Below is my wrong approach. I also tried getting rid of Trigger and using Raycast to achieve the functionality described in the beginning, but it did not work. Thanks for your help!
public class Particle : MonoBehaviour {
public Rigidbody rb;
private Vector3 init_velocity;
...
void Start()
{
rb = GetComponent<Rigidbody>();
init_velocity = InitializeVelocity();
rb.AddForce(init_velocity);
}
void InitializeVelocity() {
...
}
...
private void OnTriggerEnter(Collider other) {
Vector3 contact_point = other.ClosestPoint(transform.position);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(contact_point);
bool dummy = Physics.Raycast(ray, out hit);
Vector3 normal = hit.normal;
Vector3 force_direction = Vector3.Normalize(Vector3.Reflect(transform.position, normal));
rb.AddForce(init_velocity.magnitude * force_direction);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613040445im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is there issues with continuous collisions 0 Answers
Unity 2D colliders not triggering,Unity 2D collider not triggering 0 Answers
Why/How 2d tower of blocks collapse? 0 Answers
I need the functionality of both OnTriggerEnter and OnCollisionEnter in unity 1 Answer
How to make gridbased movement while also using collision? 0 Answers