- Home /
How to Add Knockback Force Based on What Rotation it Came From
Whenever an enemy does a certain attack and it connects, I want the player to be launched backwards. The current code I have works, but it moves them based on what X Y and Z are put as, whereas I'd like it so that force is applied based on what rotation the enemy was in when they struck the player.
public class EnemyKnockbackHit : MonoBehaviour {
public Rigidbody playerRigidbody;
public Slider healthbar;
public Vector3 moveDirection;
void OnTriggerEnter(Collider attack)
{
if (attack.gameObject.tag == "KnockbackHit") {
healthbar.value -= 20;
playerRigidbody.AddForce( moveDirection * -500f);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Answer by aldonaletto · Jun 28, 2017 at 04:23 AM
Do you mean to receive a knockback force from the direction the enemy is relative to the player? If so, just find the vector player position - enemy position and assign it to moveDirection:
void OnTriggerEnter(Collider attack){
if (attack.gameObject.tag == "KnockbackHit") {
healthbar.value -= 20;
moveDirection = playerRigidbody.transform.position - attack.transform.position;
playerRigidbody.AddForce( moveDirection.normalized * -500f);
}
}
Hey, I tried several times to invert this logic to push the enemy away from my player but it won't work - I'm totally confused now, can you help me please?
I just attached this code to my player and assigned the player rigidbody to it but everytime I hit the enemy my player is pushed back and not my enemy O.o
You must apply force to the enemy's rigidbody and calculate the direction enemy-player. Don't know how you're getting info about your enemy, but assu$$anonymous$$g that you have some reference to it in variable enemy (transform, collider, rigidbody etc.), the code should be something like this:
Vecto3 moveDirection = enemy.transform.position - player.transform.position;
enemy.rigidbody.AddForce( moveDirection.normalized * 500f);
NOTE: I've just noticed that the question and my answer used a force -500f, what would reverse the actual direction. Use a positive force (500f) ins$$anonymous$$d, as shown in the code above.
Thank you for your attention, I tried it like this:
public Vector3 moveDirectionPush;
public Rigidbody2D EnemyRigidbody;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy")
{
moveDirectionPush = EnemyRigidbody.transform.position - other.transform.position;
EnemyRigidbody.AddForce(moveDirectionPush.normalized * 8000f);
//damage dealing and effects
}
}
use AddForce on a GameObject (Enemy) to push it back by hitting it, the script is implemented in a $$anonymous$$nockback Class which is attached to the enemy but when I hit it gets teleported back and don't slide slowly back.. I don't have any idea how i could solve this, maybe with Time.deltaTime to move it slowly back? Another strange issue is that my Player only need " 300 " to get pushed back but the enemy needs "moveDirection.normalized 8000f" to get pushed back^^
Answer by kritoa · Jun 28, 2017 at 04:52 AM
Not entirely sure what you mean from the wording of your question, but I assume you are trying to get an effect so that if the enemy is moving straight at the character the character will be knocked back, and if the enemy is spinning really fast and moving slowly, then you'd want the character to be knocked sideways, and if both spinning and charging, the character should be knocked back and sideways?
If so, I think what you are looking for is https://docs.unity3d.com/ScriptReference/Rigidbody.GetPointVelocity.html , which will return the velocity of a point in world space of a rigidbody, taking into account the angular velocity. So you'd find the impact point of the enemy and your character and ask your enemy's rigidbody what the point velocity is at that point. Then you'd use AddForceAtPosition on your character to knock it in the correct direction (possibly spinning it as well!)
If you don't have a rigidbody for the enemy, respond in the comments and I can write down the math for you (but I assume you have a rigidbody since your enemy is rotating).
Your answer
Follow this Question
Related Questions
Accelerating in Turned Direction 2 Answers
Help with rotating a sprite with mouse. 2 Answers
Change movement speed based on rotation 0 Answers
Prevent Rigidbody From Rotating 3 Answers
Rotating my character only while moving in a 2D plane 3 Answers