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 /
avatar image
0
Question by spirachi77 · Jan 15, 2019 at 05:02 AM · rigidbody2drigidbody.addforceknockbackmathf.sinmathf.cos

The knockback function I made is pushing me to the left

this is my knockback function the takes in an angle, force, and hitstun. The rigidbody's velocity is set to zero before the force is applied, sin for y and cos for x and then multiplied by force. Y is multiplied by the gravity scale in the rigidbody. The problem is that it pushed the player to the left, for example if the angle is 90, the player should be sent straight up, but its pushed upwards and to the left. Any help is appreciated.

 private void KnockBack(float ang, float force, float stun)
     {
         rb.velocity = new Vector2(0,0);
         rb.AddForce(new Vector2((Mathf.Cos(ang) * Mathf.PI) / 18 * force * 100, (Mathf.Sin(ang) * Mathf.PI) / 18 * force * 100 * 5));
         hitStun = stun;
         state = "stunned";
     }

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 tormentoarmagedoom · Jan 15, 2019 at 08:42 AM 0
Share

Good day. I prettend to help you, but...

If you commence talking about angles and directions, we need a reference point or something. "for example if the angle is 90" .. from where? $$anonymous$$ake it super clear please.

Can you make 2 basic draws to easy understand what is happening and what you want to get?

PD: just a tip, to reduce to 0 a vector, you can write this

 rb.velocity = Vector2.zero;

Bye!

avatar image DCordoba · Jan 15, 2019 at 12:08 PM 0
Share

tree tips

1) the $$anonymous$$athf.Cos and $$anonymous$$athf.Sin the angle is expresed on radians, not degrees, so please convert ang to radians ang = ang*$$anonymous$$athf.PI/180f u can do it before add force, to evade do it twice... and is 180, not 18

2) whatever if u want do it inside Addforce, calculate it before do the cos calculus, so put the conversion to the angle inside the function mathf.cos some like mathf.cos(ang*$$anonymous$$athf.PI/180)*force*100

3) on the calculus of addforce u multiply the y component of the vector 5 times more than the x component... have a * 5 there, the float aren't precise so, the calculus of y component give aprox 0.00019... multiplied by 5 and not correctly divided (by 18) can be a considerable force

0 Replies

· Add your reply
  • Sort: 

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

99 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

Related Questions

How to move Rigidbody2D on Player a certain amount at a certain speed, for a dash move and knockback? 2 Answers

Deteriorating force on object? 0 Answers

Stop movement of rigid bodies 2D after collision 1 Answer

Rigidbody2D.velocity is not working. 0 Answers

Rigidbody.addforce not working with prefab 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