0
\$\begingroup\$

So when my bullet which is a Rigidbody is hitting another player which is also a Rigidbody, the bullet is getting destroyed immediately. But it seems that this isn't enough to prevent the physics to be applied. I guess that's why my player is getting pushed backward all the time when he is getting hit. I don't want this behaviour, so I tried to fix it :

I tried to lower the mass of the bullet which didn't help. I also tried to change the bullet behavior to a trigger, but the collision detection failed very often.

\$\endgroup\$
7
  • \$\begingroup\$ I don't know the scale of your project but have a look at this: answers.unity3d.com/questions/327412/… (in short, using rigidbodies for every single bullet is ineffective, using a raycast method is more of what you want to do, usually) Unless you're Battlefield or something :P \$\endgroup\$ Dec 8, 2016 at 16:45
  • \$\begingroup\$ Yeah the bullets have physics, so they are dropping and so on (It's 2D so I don't have to worry too much about performance of these bullets) \$\endgroup\$
    – TobiasW
    Dec 9, 2016 at 6:46
  • 1
    \$\begingroup\$ If it's 2D, why not pre-calculate the bullet's trajectory? Instead of using actualy physics, because that way, you can get rid of the unnecessary interactions of bullets with physics objects (usually you want physics interactions between bigger objects). If you absolutely want to use physics to simulate the bullet, then consider using a collider that will be bigger than the rigidbody (say, if the rigidbody is 1cm by 1cm, make the collider that will destroy the body of the bullet into a 1.1cm by 1.1cm collider instead.) that should do what you're trying to do. \$\endgroup\$ Dec 9, 2016 at 7:17
  • \$\begingroup\$ Mhm I will look into that, some questions for better understanding : Would you implement rockets with the same methods? And do you know about teeworlds, would you implement the projectiles of teeworlds with that technique or rather use phsysics? \$\endgroup\$
    – TobiasW
    Dec 9, 2016 at 20:35
  • 1
    \$\begingroup\$ If the bullet is supposed to push the character back, then use physics, if not, try to exclude it altogether. So, I would use physics with a rocket (because if I'm doing the collision and the pushback with it, then why not) also, there won't be a ton of rockets going around. For a minigun type of weapon though, I would rather not apply physics. Math is much less expensive in terms of CPU usage than physics is, so try to avoid using physics as much as you can. \$\endgroup\$ Dec 9, 2016 at 20:40

1 Answer 1

1
\$\begingroup\$

I'll do as TobiasW has suggested and turn my comments into an answer format.

First thing to keep in mind when using physics with bullets or short-lived objects is that there can be MANY of them. If your game is anything like Battlefield, you may want to rethink the usage of physics on every single bullet.

For CPUs, doing math is always much cheaper (in terms of performance) than doing physics calculation. So, try not to use physics if you can help it.

Also, try to use stored variables. For example, a gun will shoot a bullet towards a trajectory and calculating that trajectory every time you create a new bullet is costly. Instead, calculate it for the gun and keep that as a stored variable (if you're raycasting, store it in a Raycast variable).

If the number of bullets on screen at any given time is not that high, then it's not a bad idea to use physics, but still, try to avoid using physics calculations as much as you can. For example, if you're never going to have 100 and rarely going to have more than ten rockets on the screen at any given time, then it's not a bad idea to use physics with those. Especially if you're thinking of having "magnetic" traps (like the magnets from Worms games) which attract or deflect the rocket.

If you wish to use physics on an object but not have it push other stuff back, it's a good idea to encase the rigidbody inside a collider, so if you have a 1cm square (or cube if you're in 3D) rigidbody on a bullet, then make its collider into a 1.1cm square. This will mean that before the physics calculations of the hit can take place, the collider will do the calculations it's supposed to and then get rid of the bullet. Of course, colliders can still do a push-back like effect, but it should be infinitesimal compared to a rigidbody. Besides, you still don't want the physics calculations kicking off for a minigun that's dishing out 6000 bullets per minute.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .