- Home /
What is an effective way of colliding but passing through another surface?
Consider that I have 3 surfaces, upon collision; the first surface slightly slows a rigidbody but allows it to pass through, the second surface slows a rigidbody even more but also allows it to pass through, while the last surface would completely prevent a rigidbody from passing through and would rebound the rigidbody.
The slowing down of the rigidbody is straight forward, what I am not quite sure about is what is the best way to have these collisions triggered. I would like to use OnCollision so that I can get detailed information about the rigidbody and surface hit; such as the angle of impact, velocity at impact, collision magnitude etc. But for OnCollision I would need to use colliders and I am wondering how to make a rigidbody pass through a collider and still give collision hit information? I suppose that the other option is a raycast but in my past experience using raycasts with very fast moving rigidbodies is not very reliable. Lastly, triggers would make the passing through straight forward but they do not provide detailed hit information as far as I know.
Could someone please advise what is an effective way to achieve this.
You could use RigidBody.AddForce to slightly push it in the negative direction on all axes?
Slowing down the rigidbody is quite straight forward, what I am trying to work out is how the collision should actually be triggered. I'll edit the question to make this more clear.
Why do you need the collision information for the "slowing down" cases? From the diagram, the object simply continues on its current trajectory but at a slower speed. i.e. OnTriggerEnter () { rigidbody.velocity *= 0.5; }
The diagram illustrates an ideal situation where the projectile is being fired straight on. The collision information is useful when working out the angle of impact which is needed for deter$$anonymous$$ing the post collision speed.
Answer by Kiwasi · Mar 28, 2015 at 03:40 AM
Why not use OnTriggerEnter?
I'm not sure how this would work though. Let's say I have a wall, and a projectile can pass through it but for example other gameobjects such as a character should not be able to pass through. How would a trigger work in this case?
How complex is your interactions? You could use layer based collisions to make this work. The projectile is on a layer that hits the trigger. The player is on a layer that hits the collider.
Or you could put the trigger on the bullet.
Another option would be to raycast in front of the projectile each frame to see where it should be, and interact that way.
Do you mean having both a collider and a trigger on a collision object, and putting each on a separate layer? The only issue with a trigger is that it does not use the Collision class so a lot of the useful collision information will not be available. I have used raycasts in the past but for objects moving several hundred m/s it was not very effective, I'm not sure if this has change in Unity 5.
Ideally if I could set the projectile to ignorecollision but still be able to use OnCollisionEnter I think this would achieve the best results, but this does not work.
I have done some testing and I have had the most success with a trigger box, but there is a specific setup that is required. First of all a trigger box needs to be parented to the rigidbody. The trigger box is then responsible for turning on/off the rigidbody's collision depending on which surface it has entered. Depending on how fast the rigidbody is moving it may be necessary to make the trigger quite a bit larger than the projectile collider to get smooth results. Through some clever coding the angle of impact can be calculated but I don't think that the collision impact and other information similar to that of the collision class can be.
Answer by $$anonymous$$ · Mar 28, 2015 at 04:06 AM
If you want to get collision information & also allow an object to pass through it, simply make the collider on the surfaces act as a trigger. There is an Is Trigger checkbox in your collider component(see in inspector). Now use OnTrigger to control the behavior on collision. This time the object will be able to pass through the collider.
Your answer
Follow this Question
Related Questions
Make a rigidbody collide with this rigidbody but not that one? 1 Answer
How to make a rigidbody bullet move through a platform? 1 Answer
NavMesh Collision Detection? 0 Answers
Ball falls through the floor... why? 1 Answer
Drag & Drop GameObject with MovePosition shoots other Gameobject away in case of them colliding 1 Answer