Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 DMCH · Feb 25, 2013 at 11:37 PM · vector3addforceknockback

Knockback in 2d shooter

Hello,

I'm trying to apply a knockback effect to enemies when they are hit by a bullet. I have the basics, but it's not working exactly as intended.

Here's the current code:

 // Get vector between bullet and enemy & zero the Y
 Vector3 bulletDir = Other.transform.position - transform.position;
 bulletDir.y = 0;

 // Apply force along the normalised vector
 float force = 1000;            
 Other.rigidbody.AddForce(bulletDir.normalized * force);


If the enemy is hit on the centre, it knocks them back in the direction the bullet was travelling, but if it hits on either of the sides, it knocks them to the side. I'd like for the enemy to always be knocked back in the direction of the bullet. Suggestions greatly appreciated & thanks for reading!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DMCH · Feb 25, 2013 at 11:50 PM

Figured it out. Posting answer incase it's useful to someone else. You need the current direction of the bullet which you can get from transform.forward.

 Vector3 bulletDir = this.gameObject.transform.forward;
 bulletDir.y = 0;
 float force = 1000;
             
 Other.rigidbody.AddForce(bulletDir.normalized * force);
Comment
Add comment · Show 5 · 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 MarcioLiao · Dec 19, 2013 at 05:41 PM 0
Share

How you stop the enemies after?

avatar image DMCH · Dec 19, 2013 at 06:44 PM 1
Share

Add drag to enemy's rigidbody is probably the simplest way.

avatar image StigC · Jan 27, 2014 at 07:43 AM 0
Share

Where do you put this code? I have a PistolScript, a NPCLogicScript, and a HealthScript for the NPC. Where should I implement this function?

avatar image DMCH · Jan 27, 2014 at 02:00 PM 0
Share

I put it in the script attached to the bullet. Sounds like you might be using raycasting to detect hits, so you probably should apply force to the NPC along the player's current rotation.

avatar image Rexor4321 · Sep 14, 2017 at 09:59 AM 1
Share

Thanks man! $$anonymous$$ore people should do like this when they find a solution

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.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

I need to add a force to an object based on the local vector of the object that is hitting it and not the world coordinates or local coordinates of the object being hit. 1 Answer

Fire Knockback, Vectors, Directions 1 Answer

Jumping problem to do with /= magnitude changing the vector3 direction 1 Answer

How do I add diagonal force? 0 Answers

Advice on keeping distance using AddForce 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