• 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
Question by KlausAidon · Aug 31, 2012 at 09:19 PM · collisionaienemypushback

Player pushback when collide with enemy

I want a create a effect similar to Megaman in my platform when you hit an enemy. As in, the player gets pushed back a bit when he takes damage. My player is a rigid body, so I know I should use the AddForce, probably using an Impulse, however I'm unsure if I'm using this right as nothing is happening. Does anyone have an example of Addforce being used for something similar? Here is my test function.

public void takeDamage() { //apply actual force rigidbody.AddForce(-Test, ForceMode.Impulse); }

Where -Test is the player's current velocity mirrored.

Comment
aldonaletto
Rodrigoj10

People who like this

2 Show 0
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

  • Sort: 
avatar image

Answer by aldonaletto · Aug 31, 2012 at 11:43 PM

You should not use the player velocity inverted to push it back, because nothing will happen when the player velocity is zero. There are some well defined strategies to apply the impact, depending on what is hitting the player:

  • If your player is a rigidbody, you don't need to do anything when the enemy hits the player with another rigidbody (the projectile): physics will generate the appropriate reactions to both, the player and the projectile, and your only job is to choose suitable masses for each one - hitting a mass 1 player with a mass 1 bullet will throw it back too far, thus the bullet and the player must have a reasonable mass ratio (like player mass equal to 10 times the bullet mass, for instance).

  • Things are different when the enemy hits the player with a raycast shot: since there's no actual projectile, you must simulate the impact in the shooting code, like this:

Enemy weapon script:

var impactForce: float = 20;

function RaycastShot(){ // function that actually does the shot var hit: RaycastHit; // shoot in the weapon forward direction: if (Physics.Raycast(transform.position, transform.forward, hit)){ if (hit.rigidbody){ // if any rigidbody hit... // apply impactForce in the shot direction: hit.rigidbody.AddForce(transform.forward * impactForce); } } }

  • If the enemy hits the player with a rocket, grenade or other explosive device, you should apply an explosive impact force to all rigidbodies around. Usually, this is done in the explosion script: the rocket suicides and instantiates an explosion when hitting something, and the explosion script applies the explosive force to all nearby colliders according to how close they are to the explosion:

Explosion script:

var radius = 5.0; var power = 50.0;

function Start () { var explosionPos : Vector3 = transform.position; // find nearby colliders: var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius); for (var hit : Collider in colliders) { // if any of them have a rigidbody... if (hit && hit.rigidbody){ // apply a ExplosionForce to it: hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0); } } }

NOTE: An interesting side effect of these strategies is that any rigidbody hit by the enemy shots will react properly, no matter if it's a crate, a barrel, or whatever (provided that it has a rigidbody component, of course).

EDITED: I was too sleepy to notice that the push back force should be applied when the player collides with the enemy - maybe simply reversing the player velocity could do the job, like below:

public void takeDamage() { 
  // revert player velocity:
  rigidbody.velocity *= -1;
}

Comment
Rodrigoj10

People who like this

1 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 KlausAidon · Sep 03, 2012 at 07:36 PM 1
Share

Works perfectly, thank you! Also thanks for all the other info. At the moment I was just working on getting the player to get knocked back when he runs into an enemy, but knowing about bullets, and explosions will likely help me later on when I add projectiles, so thanks!

avatar image

Answer by MathieuBarbier · Aug 31, 2012 at 10:42 PM

You can use the velocity to do this : velocity.x = -1 or something like that (use a temp vector 3 to stock velocity).

Comment

People who like this

0 Show 2 · 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 KlausAidon · Aug 31, 2012 at 10:49 PM 0
Share

Not sure I understand, can you give more detail?

I already know I'm using velocity.

avatar image MathieuBarbier · Aug 31, 2012 at 10:51 PM 0
Share

Like this post : http://answers.unity3d.com/questions/260191/getting-rigidbody-to-jump-by-changing-the-velocity.html

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

AI enemy bouncing with no collision 1 Answer

sample of AI enemy collision 1 Answer

Enemy Ai Problem with the collider and gravity 1 Answer

Question about AI in MMORPG 1 Answer

Stay Back! 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges