I am making a 2D game and have a basic weapon which I decided to call a pistol. I want to make a shotgun next and I have all of the work done except for the fact that they are all spawned in the same place and they all collide with each other.
I've had a few ideas like trying to turn off collisions for the rigidbodies (Didn't work), applying force without a rigidbody (Didn't work). I've done one game before this which was through a tutorial so this is my first real game and I just need help. If you need more details I can always send more. Thank you.
CodePudding user response:
CodePudding user response:
The script you need is the following line. In this line, Unity physics renders one collider ineffective against another.
Physics2D.IgnoreCollision(collider, ignoreCollider);
Also, with the algorithm below, you can neutralize all colliders at the moment of the bullet shot.
[SerializeField] private GameObject bulletType;
private List<Collider2D> _tempCollides;
void ShotBullets()
{
_tempCollides.Clear();
for (int i = 0; i < 4; i )
{
// Instantiate and catch all colliders in a List
var collider = Instantiate(bulletType, AnyPlace, AnyRotation).GetComponent<Collider2D>();
_tempCollides.Add(collider);
}
foreach (var _collider in _tempCollides) // For Every Bullet Collider...
{
foreach (var _subCollider in _tempCollides) // .. Define to Ignore other Colliders..
{
if (_collider != _subCollider) Physics2D.IgnoreCollision(_collider, _subCollider);
}
}
}