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 PinkMustard · Sep 26, 2014 at 04:00 PM · rigidbody2dforce

Enemy Knockback

Greetings!

How would I go about to create a knock-back from an enemy that sends the player flying in the correct direction?

I already figured out how to push the player in the correct x-direction:

     Vector2 myPosition = transform.position;
     Vector2 dir =  myPosition - enemyPos;
     dir.y = 0;
     
     rigidbody2D.AddForce(dir.normalized * force);

If I want to push the player a greater distance in the x-axis, I have to crank up the force variable to about 1500 which makes the player almost teleport, so I figured a bit of force in the y-axis would make the character fly further.

I'm kind of looking to create a knock-back like in this image

alt text

Thanks for your help!

dump.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 Habitablaba · Sep 26, 2014 at 04:51 PM 1
Share

You've pretty much said it already. You just need to add force in the Y direction. You're currently setting dir.y to 0, have you tried changing that to something higher than 0?

avatar image PinkMustard · Oct 01, 2014 at 11:36 AM 0
Share

I tried to change the dir.y to a constant number. 0.5f is a decent number. When I inserted this line of code:

 Debug.DrawRay(enemyPos, dir.normalized*force, Color.green);

The scene view showed a bright green line in a perfect angle.

But when adding the force, like above, the character goes almost straight up in the air, e.i far on the y-axis but nothing on almost nothing on the x-axis

Also when setting the force to a really high number the character moves in an L kind of way. What I mean by that is that the character is first moved in the x-axis, and then in the y-axis.

What am I doing wrong?

1 Reply

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

Answer by NoseKills · Oct 01, 2014 at 04:07 PM

If you first change dir.y and then normalize, you might get wildly inconsistent results depending on what the dir.x value was. (The end result of normalizing (2f, 0.5f) and normalizing (0.1f, 0.5f) is very different)

It might help a little if you do it in different order.

     Vector2 dir =  (myPosition - enemyPos).normalized;
     dir.y = 0.5;
  
     rigidbody2D.AddForce(dir * force);

And then find a suitable dir.y

Still the root of the problem is deciding what you want to happen in each situation mathematically and what are all the possible use cases.

Since you are not using the magnitude of the dir distance vector for anything (you are normalizing it), you might as well type in some vector that gives you the desired trajectory and just flip the x-coordinate based on which side the enemy is on.

 Vector2 pushForce = new Vector2(100, 50);
 
 if (myPosition.x > enemyPos.x)
 {
    pushForce.x = -pushForce.x;
 }
 
 rigidbody2D.AddForce(pushForce);

Comment
Add comment · Show 3 · 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 PinkMustard · Oct 03, 2014 at 11:40 AM 0
Share

Thank you so much Nose$$anonymous$$ill for your answer! I figured out what caused the querky L movement. Right after my character gets knock backed I set the velocity to the x and y velocity of the rigidbody (It's in the FixedUpdate so it's fine).

avatar image Creative Inventory PinkMustard · Oct 29, 2015 at 03:45 PM 0
Share

I keep on getting errors with my code!!! Could you please help! @Pink$$anonymous$$ustard and @Nose$$anonymous$$ills

avatar image PinkMustard Creative Inventory · Oct 30, 2015 at 02:09 PM 0
Share

I'm sorry @Creative Inventory you need to provide more informations for us to be able to help you. I must also inform you that this code was part of a project that I worked on a year ago. I haven't touched Unity since then and lost the project files so I'm not sure how much of an asset I will be.

Post a little more information about your code and somone might be able to help you

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

28 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

Related Questions

How can I get 2 rigidbody2d objects to "share" physics? 1 Answer

How not to lose speed when collided with wall? 2 Answers

Trying to launch a rigidbody towards mouseposition but nothing makes it work. 1 Answer

how to make force from mousepointer constant ? 0 Answers

I am a begginer trying to make a simple 2d platformer. Need help with rotation and jump height. 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