- Home /
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!
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.
Thanks for this! I ended up solving the problem. :D I appreciate the help.
Totally fixed it.
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!
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
Follow this Question
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