Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Oct 30, 2015 at 05:05 PM · c#addforceknockback

Knockback player on collision

I want to create a game where if my player collides with an enemy the player gets knocked back/ pushed back. I've looked on Google and tried different codes but none of them meet my satisfaction. Most of the codes I used included AddForce. I want it to be something like this: alt text

Thank You!

knockback.png (6.2 kB)
Comment
Add comment · Show 2
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 Paulo-Henrique025 · Oct 30, 2015 at 05:45 PM 0
Share

Are you using Character Controller or Rigid Body to develop your characters?

avatar image Creative Inventory Paulo-Henrique025 · Oct 31, 2015 at 12:17 PM 0
Share

I'm using a rigid body 2d

2 Replies

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

Answer by lloladin · Nov 14, 2015 at 02:44 PM

i normally just use velocity like this

 gameobject.GetComponent<Rigidbody2D>().velocity = new Vector2 (x,y);

the only problem with this is that the object will move for a while after being knocked back a way to solve that would be use a timer to change the duration of the knockback or like i do simply change the velocity to 0 when the object hits the ground if the object is being knockedbacked so make a bool that you check/uncheck depending on whether the object is being knocked back

Code for the object that knocking things back

 public bool knockedBack
 
 void OnTriggerEnter2D (Collider2d other)
 {
 other.GetComponent<Rigidbody2D>().velocity = new Vector2 (x,y);
 knockedBack = true
 }

and for the object thats being knocked back

 public bool grounded
 
 if (grounded && GetComponent<Other Script>().knockedBack == false)
 {
 GetComponent<Rigidbody2D>().velocity = new vector 2 (0,0)
 }
 

hope this help

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 Creative Inventory · Dec 21, 2015 at 11:02 AM 0
Share

Thank you but I want to do something like this @lloladin

avatar image
0

Answer by Statement · Oct 31, 2015 at 01:03 PM

So I don't know which methods you've used but it looks from the picture that a simple AddForce won't cut it because you want rotation as well. Use AddForceAtPosition and set the position slightly off center to give it some spin. Try using ForceMode2D.Impulse.

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 Statement · Oct 31, 2015 at 01:04 PM 0
Share

For 3D, see the Rigidbody docs. The methods are called the same.

I also have an example 2d playable demo/full source that uses AddForceAtPosition to simulate top down wheel physics in this answer.

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

34 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

Related Questions

Why doesn't my knockback function apply force to it's target? 1 Answer

2D Knockback problem 2 Answers

Enemy watches player and shoots but bullets will not project 0 Answers

Knockback direction always the same 2 Answers

Why can't I get ball movement with this script? 1 Answer


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