- Home /
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:
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:
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)
but why adding small force over time? just add big force once.
Answer by unity_ek98vnTRplGj8Q · Aug 05, 2020 at 05:00 PM
You've got a couple problems here -
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. **
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.
Your answer
Follow this Question
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