- Home /
How to make a rigidbody bullet move through a platform?
my character is shooting a gun. I want the bullets to shoot up through platforms but collide when moving down. They are arcing, mortar type bullets, and I would like to tie their platform collision to their velocity in y.
I can't do Physics2D.IgnoreCollision(c1, c2) because it will still impact at least once. I can't set it to kinematic, because it still needs to collider with enemies. I can't do IgnoreLayerCollision(layer1, layer2) because then bullets moving down will also be affected. I could have 2 layers, one for bullets moving up and 1 for bullets moving down. That is all I can think of.
I would like to be able to just set a layermask value on the prefab. Is there any way to make specific colliders/gameobjects ignore specific layers?
Answer by jfjordanfarr · Jul 03, 2015 at 07:28 AM
Okay, this might not be a 100% perfect answer, but how about this?
Given that bullets tend to continue moving in the same direction with essentially constant velocity, how about you go into the bullet script and start checking contact point "normals" (see http://docs.unity3d.com/ScriptReference/ContactPoint2D-normal.html)? The "normal" is perpendicular to the point of contact, and helps you figure out the orientation of two objects that end up in a collision. If there is a particular y value on a contact point's normal (for collisions above the object in question I believe that value is -1), you could set the rigidbody's velocity to be exactly the same as it was in the last frame (or last FixedUpdate), hopefully enabling you to pass through ceilings.
Here's one possible code block for how I'd go about this:
void OnCollisionEnter2D(Collision2D col)
{
if (col.contacts[0].y == -1)
{
RB.velocity = previousVelocity;
}
}
You'd need to rig up the bullet's rigidbody as RB or change RB to whatever variable you have, and set previousVelocity somewhere like FixedUpdate. Adding gameObject tag or layer checks could also help eliminate bullets passing through enemies if the bullet came from below them.
You could also test this on CollisionStay2D as well if need be, and even check more contact points' y value than just the first contact point.
I have done similar things when not using rigidbodies, where I essentially abandon the 2d physics system and write my own collision.
This time, however, I am trying to stick with it.
That said, doing your described method, you will not be able to pass the bullet object through the platform. If OnCollisionEnter is being called, then one of those is a non-kinematic rigidbody. If the bullet is non-kinematic (which it should be to arc and bounce it), it will not be able to pass through the platform (kinematic or otherwise). Setting the velocity will not help.
Your answer
Follow this Question
Related Questions
Ignore Collision Layer Ignores also other layers 1 Answer
Make a rigidbody collide with this rigidbody but not that one? 1 Answer
What is an effective way of colliding but passing through another surface? 2 Answers
OnCollisionEnter doesn't work if enemies and allies have rigidbody ? 1 Answer
Rigidbody: how to get collisions for the current frame ? 0 Answers