Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 unity_vVZc-PQLVkDP-w · Aug 05, 2020 at 11:14 AM · knockbackforces

Problem with enemy Knockback.

Hi guys, Iam pretty new to unity and I have problem with knockback. In my game I have player and enemy. When Player hit enemy, enemy shoud have some knockback. For knockback Iam using this function:alt text

In general I have while loop, which is monitoring time. And in the meantime it adds force to RigidBody. Duration = 1, knockbackSpeed.x = 3, knockbackSpeed.y = 1, demageDirection is just 1 or -1.

My problem is that the knockback is so wierd. I thought that will be problem in the movement, beacause It adds velocity to enemys rigidbody so maybe it will disturb each other:alt text

I know that this description and screenshot will propably not help you, but at least do you have any ide What shoud I use for knockback? (Force or velocity, some other)

knockback.png (16.0 kB)
move.png (19.5 kB)
Comment
Add comment · Show 1
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 $$anonymous$$ · Aug 05, 2020 at 11:55 AM 0
Share

but why adding small force over time? just add big force once.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_ek98vnTRplGj8Q · Aug 05, 2020 at 05:00 PM

You've got a couple problems here -

  1. Using a while loop like this will not apply a force over time. The while loop will run all the way through every frame, so the end result will be a large amount of force being applied all at once. I could help you reorganize your code to successfully apply force over time, but I think there is a better solution for you. **

  2. You are setting your velocity every frame while also trying to add a force. The result of this is that the force is immediately canceled out. If you apply a force during one frame, then the physics system will calculate a change in velocity and apply it to the rigidbody. But if you then set the velocity the next frame, you are effectively overwriting whatever velocity change the physics system just calculated for you.


For damage knockback I recommend keeping track of this yourself rather than handing it over to the physics system. The code is simple - just keep track of your own velocity vector due to outside forces and add it to your rigidbody velocity every frame. Then you can scale this vector down over time. Here is the basic code, but you will have to fit this in to your own code

 public class MyCharacterController : Monobehavior{
     private Vector3 outsideForces;
 
     //You can adjust this
     public float forceDecayRate = 1f;
 
     public void AddForce(Vector3 force){
         outsideForces += force;
     }
 
     void Update(){
         //blah blah blah
         Move();
 
         //Decay force over time
         outsideForces = Vector3.Lerp(outsideForces, Vector3.zero, forceDecayRate*Time.deltaTime);
     }
 
     private void Move(){
         //....
 
         Vector2 newVelocity = Vector2.zero;
 
         if(targetPosition.x > transform.position.x){
             newVelocity = new Vector2(speed, rb.velocity.y);
         }
         else if(targetPosition.x <>> transform.position.x){
             newVelocity = new Vector2(-speed, rb.velocity.y);
         }
 
         rb.velocity = newVelocity + outsideForces;
     }
 }

When you need to apply knockback, just call the AddForce() method in your controller script with the desired force vector. You can even use this to apply multiple knockbacks at the same time.

Comment
Add comment · 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

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

132 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 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

Allow collision but take no force from it, not increasing object mass 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

Knockback in 2d shooter 1 Answer

rb.AddForce-transform.right.. looks like teleporting how to fix?? 1 Answer

Knock Back only moving player up and down (vertical Y axis) 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