• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smugleafdev · Jan 03 at 05:00 PM · physicscollision detection

How can I make only a killing blow apply physics?

I want all enemies to be immune to physics unless the hit would kill them. In that case, I would like the physics of getting hit to apply. If there was a magic FrameBeforeCollision() method I could use, I could just turn off kinematics and be done, but Unity is not that nice.

I could potentially do this by having every projectile check in FixedUpdate() if there will be a collision in the next update, pass the damage value to the enemy to see if that hit would kill it, and if so turn off kinematics. Thus, the next update will be a collision, I can apply damage reducing the HP to 0, and then the physics of the hit will send the enemy flying. My worry here is if I have a very quick moving enemy and a collision on the edge of the collider, the prediction could be wrong since my projectiles are not instantaneous bullets.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TimBur · Jan 03 at 10:29 PM

I'm guessing a little bit, because I don't fully understand your concept, but I think your suggested solution is going in the right direction. Though I think there is a slightly simpler way to do what you want to do. If most hits don't kill, then the default should be to keep physics turned off. If a hit would kill, then you apply physics in the moment of that killing blow, through scripting. And the various OnTrigger / OnCollision events are pretty close to that magic method that you're asking for.

In outline, I suggest you make all your colliders into trigger-type colliders and write some logic into the OnTriggerEnter method. Also, make sure all of your enemies have a kinematic rigidbody. When a bullet hits an enemy, the enemy's OnTriggerEnter method would run. In that method, you would calculate damage, and change the enemy health accordingly. If the enemy health is below zero, then you could switch the enemy's rigidbody to dynamic, and use either AddForce or AddForceAtPosition to apply a knockback to the enemy. All of this would happen within OnTriggerEnter.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smugleafdev · Jan 11 at 12:52 AM 0
Share

and use either AddForce or AddForceAtPosition to apply a knockback to the enemy

This is what I'm doing, but my "manual hit" isn't quite the same as if I had kinematics off and hit the target with an attack. It seems good enough I guess, but I'd be happier if I could perfectly simulate re-applying the hit.

Currently I have the bullet collision call this method on the enemy controller script, passing the bullet's damage value and the whole collision:

 public void DamageEnemy(int damage, Collision collision) {
         enemyCurrentHealth -= damage;
         if (enemyCurrentHealth <= 0) {
             isDead = true;
             enemyRigidbody.isKinematic = false;
             collisionImpulse = collision.impulse;
             contact = collision.GetContact(0);
         }
     }

Then in FixedUpdate I apply this hit:

 void FixedUpdate() {
         if (collisionImpulse != Vector3.zero) {
             // TODO: This force isn't quite right, but it's close.
             enemyRigidbody.AddForceAtPosition(-collisionImpulse / 2, contact.point, ForceMode.Impulse);
             collisionImpulse = Vector3.zero;
         }
     }

Any idea what I might be doing wrong?

avatar image TimBur smugleafdev · Jan 12 at 06:44 PM 0
Share

Hmm. That code looks pretty reasonable to me. A couple questions and thoughts:

You say the "manual hit" isn't quite the same as when kinematics are off. That's interesting. How is the "manual hit" different from what you want?

Also, where are you getting your collision info? If you're using OnTrigger-type events, those only give you collider data. It was my understanding that, to get collision info, like contact points, you need OnCollider-type events.

An aside: If you are applying some sort of "constant" force, then you do need your AddForce to be in FixedUpdate. If the force is from an instant-ish event, and uses Impulse mode, you can call AddForce from anywhere. The physics engine will then remember the call, and apply the impulse during the next physics update cycle. So you could simplify your code by putting the call to AddForceAtPosition into your DamageEnemy method. That won't fix the problem of the hit physics not being what you want, but it would simplify your code a little bit.

avatar image smugleafdev TimBur · Jan 13 at 05:31 AM 0
Share

I'm passing the collision from OnCollisionEnter, recording the collision.impulse, and passing that. I think my code above covers all the details minus that the collision is from OnCollisionEnter. I'm only using the first contact point though. Maybe that's why it's different?

The main difference between my "manually applied hit" and turning off kinematics is noticeable. I've tested many times between the two. The "manual" one is a dramatic, almost comical hit by comparison.

Show more comments
avatar image
0

Answer by ddooms · Jan 04 at 01:30 AM

The simplest solution I feel would be to disable kinematics any time a projectile is NEAR the player (close enough to hit) while also having a health that could be lethal if hit. That's far less complicated than prediction. The only problem could be frame rate - physics objects can cause issues with lag and the result could be errored. That said, I think it might be good enough for your use case.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smugleafdev · Jan 11 at 01:24 AM 0
Share

If two bullets were simultaneously near an enemy and they disabled the kinematics, it could be that the one that hits first doesn't kill the enemy, but now the enemy gets knocked back because kinematics was disabled. Actually moving enemies in my game before they are dead would be a huge imbalance.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

189 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to hit objects in VR 1 Answer

Best collision detection method? 2 Answers

How do I "remove/disable" collision? 3 Answers

Physic based golf game - ball bouncing off the connection of colliders on flat surface 2 Answers

Platform collisions only from above in 2D 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges