I’m currently working a top-down space shooter like JamesTown:
My main problem at the moment is the fact that the collision checking between bullets is taking up like 90% of all performance. There only needs to be collision checking between the bullets and the ships. Not between bullet and bullet.
here’s the setup:
Bullet:
Rigidbody 2d
Box collider 2d
isTrigger = true;
Ships:
Rigidbody 2d
Box collider 2d
isTrigger = false;
The bullet’s movement are all set using rigidBody2d.addForce();
Now I basically have 3 questions:
Can I disable collision checking between bullet and bullet?
Is this a good setup for having tons of bullets onscreen?
When I want to stop the bullets, can I say rigidbody2D.velocity = new Vector2(0, 0); or should I subtract the bullets movementVector by using addforce() again?
1 - Put them on different layers and disable interaction between those layers in the collision matrix
2 and 3 - pool your bullet objects so you don’t have to create/destroy constantly and I definitely wouldn’t move them with physics. I’d make the rigidbody kinematic and just move the transform around directly.
do you mean I need to put every bullet on a different layer? Otherwise I don’t quite get what you mean.
The problem isn’t that the players bullets shouldn’t have collision checking with the enemy bullets.
The problem is that the player bullets shouldn’t have collision checking with the other player bullets.
I already use objectPooling, thanks for the tip though.
Will try making the bullets kinematic and changing the way I move them. Thanks!
I don’t know why (perhaps because I’m using unity’s 2d stuff?)
but when I change the bullets position manually like you said, I actually lose 50% frameRate compared to using rigidbody2d.velocity = new Vector2(x,y);
Also tried transform.Translate, but this gives the same frameRate as just changing transform.position.
Why make the bullets kinematic and move them with transform? Wouldn’t that miss collisions if the bullets were moving quickly?
I’m kind of new to Unity. Just curious why he should not using a regular rigidbody without gravity and doing rigidbody.AddForce(force, ForceMode.Velocity) once when the bullet is fired.
That’s true - you would have to compensate for that. My main motivation was avoiding any kind of physics calculation. In hindsight, I don’t know that the net gains would outweigh the extra code you’d have to write. So maybe not the best advice ultimately.