- Home /
How to make arch knockback
So I want my character to be knocked back when it's hit by an enemy. I tried to use this tutorial, https://www.youtube.com/watch?v=sdGeGQPPW7E. But it did not have the desired affect I was looking for. This is the curve I am getting when the player gets knocked back with the code below. (don't mind the height) ![alt text][1]
and this is what I would like. ![alt text][2]
This is the code I currently have for the first curve.
if(knockbackCount <= 0)
{
rb.velocity = new Vector2(input * speed, rb.velocity.y);
} else
{
if(knockFromRight && isGrounded == true)
{
rb.velocity = new Vector2(-knockback, nothing);
}
else if(!knockFromRight && isGrounded == true)
{
rb.velocity = new Vector2(knockback, nothing);
}
else if(knockFromRight)
{
rb.velocity = new Vector2(-knockback, knockback);
}
else if(!knockFromRight)
{
rb.velocity = new Vector2(knockback, knockback);
}
knockbackCount -= Time.deltaTime;
}
Any help is welcomed, :) thank you! [1]: /storage/temp/169573-not-right-curve.png [2]: /storage/temp/169574-desired-curve.png
Sorry Forgot to add this code which is on the enemy.
if (other.tag == "Player") { var player = other.GetComponent(); player.knockbackCount = player.knockbackLength;
if(other.transform.position.x < transform.position.x)
{
player.knockFromRight = true;
} else {
player.knockFromRight = false;
}
}
Answer by NullOverrideX · Oct 21, 2020 at 07:25 AM
I think the reason why it has the not so desired curve is because you are slowing down in the air. If the horizontal movement stops in the air, it will fall straight down. Maybe you can try detecting if you are in the air; if you are, then continue to add a constant rb force until it touches the ground.
Hope this helps, I'm just 9 and found this online
This wasn't the answer but made me have a lightbulb in my head to get the answer, thanks so much!
This is the code that was the answer for anyone co$$anonymous$$g along this post
if(knockbackCount <= 0)
{
rb.velocity = new Vector2(input * speed, rb.velocity.y);
} else
{
if(knockFromRight)
{
StartCoroutine(KnockbackFunctionRight());
}
else if(!knockFromRight)
{
StartCoroutine(KnockbackFunctionLeft());
}
knockbackCount -= Time.deltaTime;
}
public IEnumerator KnockbackFunctionRight()
{
rb.velocity = new Vector2(-knockbackX, knockbackY);
yield return new WaitForSeconds(0.075f);
rb.velocity = new Vector2(-knockbackX2, knockbackY2);
yield return new WaitForSeconds(0.075f);
rb.velocity = new Vector2(-knockbackX3, knockbackY3);
}
Answer by KGBemployee · Mar 04, 2021 at 09:13 PM
I would suggest something like this that saves you from using coroutines, just remember to take control away from the player. Call the Knockback()
function from wherever you see fit (enemy script, a player health controller, etc.)
void Update()
{
if (!isDead)
{
if (knockbackCounter <= 0)
{
//handle player movement according to your preference
}
else
{
knockbackCounter -= Time.deltaTime;
}
animator.SetBool("isGrounded", isGrounded);
animator.SetFloat("YVelocity", theRB.velocity.y);
animator.SetFloat("MoveSpeed", Mathf.Abs(theRB.velocity.x));
}
}
public void Knockback(Transform other)
{
knockbackCounter = knockbackLength;
animator.SetTrigger("Hurt");
theRB.velocity = new Vector2(0f, theRB.velocity.y);
if (transform.position.x < other.position.x) //attack is comming from the right
{
theRB.AddForce(Vector2.left * knockbackForce, ForceMode2D.Impulse);
theRB.AddForce(transform.up * knockbackVerticalForce, ForceMode2D.Impulse);
}
else //attack is comming from the right
{
theRB.AddForce(Vector2.right * knockbackForce, ForceMode2D.Impulse);
theRB.AddForce(transform.up * knockbackVerticalForce, ForceMode2D.Impulse);
}
}
}
Answer by ChristopherCarvalho · Dec 29, 2021 at 01:39 PM
After numerous tests to see why the rigidBody did not act going backwards basically forming an arc with these two functions: rigidbody2D.addForce(Vector2.up AnyValue, forceMode.Impulse); rigidbody2D.addForce(Vector2.rigth AnyValue); I found that there was a crash for using rigidBody.velocity as an actuator in moveInput, that is, there was a conflict, the speed verification action on the x and y axis was disabled and it worked! I hope it helps, I spent 3 days and a lot of talking with Gd hahaha. Glory to Adonai!
Your answer
Follow this Question
Related Questions
Need help with knockback in C# Please! 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers