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
2
Question by Condore · Aug 29, 2012 at 12:45 AM · c#enemyaddforcemeleeknockback

Need help with knockback in C# Please!

Hey guys, I'm currently working on a knock back system for my Third-Person Melee game within Unity and I was just wondering if someone could point me in the right direction.

I've been trying for the past couple hours to try and code it without any help and I've searched all over UnityAnswers for a solution but, all I keep finding is how to addForce in Java rather than C#.

So...

What I'm trying to do exactly is have an Enemy be pushed back when my character swings her sword and the Collider attached to the sword hits the enemy. I already have the collider dealing damage to the Enemy with a OnTriggerEnter Function, I just don't have any idea how to use addForce or ForceMode.Impulse to knock back my Enemy..

Any help would be appreciated!

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

2 Replies

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

Answer by aldonaletto · Aug 29, 2012 at 03:07 AM

You can use AddForce, but only if the enemy is a rigidbody (if it's a CharacterController, a different approach must be used)
For a melee, we must somehow calculate the direction of the impact: a simple solution is to use the direction melee->enemy, but ignoring the vertical component (pushing the enemy against the ground usually doesn't produce a decent effect).
That's an example (attach this to the melee):

   public float force = 500; // adjust the impact force
 
   void OnTriggerEnter(Collider other){
     Vector3 dir = other.transform.position - transform.position;
     dir.y = 0; // keep the force horizontal
     // try to get rigidbody of other object
     Rigidbody rb = other.GetComponent<Rigidbody>();
     if (rb){ // use AddForce for rigidbodies:
       rb.AddForce(dir.normalized * force);
     } else { // use a special script for character controllers:
       // try to get the enemy's script ImpactReceiver:
       ImpactReceiver script = other.GetComponent<ImpactReceiver>();
       // if it has such script, add the impact force:
       if (script) script.AddImpact(dir.normalized * force);
     }
   }

If the enemy is a CharacterController, you must add the script ImpactReceiver below to its prefab, so that all enemies will be able to receive impacts like rigidbodies do:

 using UnityEngine;
 
 public class ImpactReceiver: MonoBehaviour {
   
   public float mass = 3.0; // define the character mass
 
   Vector3 impact = Vector3.zero;
   CharacterController character;
 
   void Start(){
     character = GetComponent< CharacterController>();
   }
 
   void AddImpact(Vector3 force){ // CharacterController version of AddForce
     impact += force / mass;
   }
 
   void Update(){ 
     // apply the impact effect:
     if (impact.magnitude > 0.2f){
       character.Move(impact * Time.deltaTime);
     }
     // impact energy goes by over time:
     impact = Vector3.Lerp(impact, Vector3.zero, 5*Time.deltaTime);
   }
 }

NOTE: If the melee is the trigger, add a kinematic rigidbody to it - moving triggers need it to work fine.

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 Condore · Sep 07, 2012 at 08:28 PM 1
Share

Thanks for this! I ended up solving the problem. :D I appreciate the help.

Totally fixed it.

avatar image SweatyChair · Nov 06, 2018 at 03:52 AM 2
Share

Would be great if you can format your code.

avatar image aldonaletto SweatyChair · Nov 16, 2018 at 08:51 PM 0
Share

Formatting done!

avatar image
3

Answer by kristercollin · Aug 29, 2012 at 01:52 AM

Well, you could decide to get a bit tricky on this one.

You can use Rigidbody.AddExplosionForce to perform the knockback. If you look at it's script, you'll see that it automatically finds every gameobject within a certain radius and stores those in an array, then applies a force to them relative to it's gameobject's position.

What you can do is hijack that process and, instead, collect only the gameobjects that the sword has collided with, then apply the force to those. You could probably do this as a part of your OnTriggerEnter doings, actually.

Hope that helps!

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 Condore · Sep 07, 2012 at 08:30 PM 1
Share

I was thinking of using this before, and it sounds like it would work a lot better for the other character state I have which has a wide arc/ AOE attack.

How exactly would I hijack it to store all the gameObjects the sword collides with?

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

10 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

Related Questions

Distribute terrain in zones 3 Answers

How to make arch knockback 3 Answers

knockback 3d c# 1 Answer

Multiple Cars not working 1 Answer

Deteriorating force on object? 0 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