- Home /
How do I effectively perform KnockBack whenever my character gets hit by an enemy/ harmful object?
Howdy! I've been working on the knockback function/implementation for my 2D sidescrolling shoot em' up game for about 2 weeks now and I was hoping anyone could help me figure it out? I've been doing nonstop research online, but no matter what I find, my character still seems to just be bouncing up a tiny bit, and not up and to the left (or right, depending on which side the enemy is on) whenever he gets hit by the enemy, I would like my character's knockback to be similar to that of the original Sonic Sega Genesis trilogy , it just feels so smooth, even though you had just been hit by an enemy. The way I have my code set up now is: My Health script has a Hurt function and this hurt function communicates with the PlayerControls2 script to execute the knockback function, but, again, its just bouncing my character in the air whenever he hits an enemy, almost like he's jumping whenever he hits the enemy. Below I have my Health script, and my PlayerControls2 script, thank you for your time and for a potential answer :) P.S: If you need any additional code/ anything you can think of, or need a video of the bug, I will post it here ASAP
***//Health*************************************************************************
public int health = 5;
public int numOfCrystals;
private PlayerControls2 player;
public Image[] crystals;
public Sprite fullCrystal;
public Sprite emptyCrystal;
void Start()
{
player = FindObjectOfType<PlayerControls2> ();
}
void Update()
{
if(health > numOfCrystals)
{
health = numOfCrystals;
}
for (int i = 0; i < crystals.Length; i++) {
if (i < health) {
crystals [i].sprite = fullCrystal;
} else {
crystals [i].sprite = emptyCrystal;
}
if (i < numOfCrystals) {
crystals [i].enabled = true;
} else {
crystals [i].enabled = false;
}
}
}
public void Hurt()
{
health--;
player.knockBack ();
if (health <= 0) {
SceneManager.LoadScene (SceneManager.GetActiveScene().buildIndex);
}
}
void OnCollisionEnter2D (Collision2D collision)
{
Enemy enemy = collision.collider.GetComponent<Enemy> ();
if (enemy != null) {
Hurt ();
}
}
}
**//PlayerControls2****************************************************
public float speed;
public float jumpForce;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
public int extraJumpValue = 0;
public float fallMultiplier = 2.5f;
public float lowJumpMultiplier = 2f;
public float knockBackForce = 10;
public bool knockBackNow;
/*
public float knockBackLength;
public float knockBackCount;
public bool knockFromRight;
*/
//public Transform currentCheckpoint;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
private int extraJumps;
void Start()
{
extraJumps = extraJumpValue;
rb = GetComponent<Rigidbody2D> ();
}
void FixedUpdate() //used to manage all the physics in the game
{
isGrounded = Physics2D.OverlapCircle (groundCheck.position, checkRadius, whatIsGround);
if (knockBackNow == false) {
moveInput = CrossPlatformInputManager.GetAxis ("Horizontal");
rb.velocity = new Vector2 (moveInput * speed, rb.velocity.y);
} /*else {
if (knockFromRight) {
rb.velocity = new Vector2 (-knockBack, knockBack);
}
if (!knockFromRight) {
rb.velocity = new Vector2 (knockBack, knockBack);
}
knockBackCount -= Time.deltaTime;
}
*/
if (facingRight == false && moveInput > 0) {
Flip ();
} else if (facingRight == true && moveInput < 0) {
Flip ();
}
}
void Update()
{
if (isGrounded == true) {
extraJumps = extraJumpValue;
}
if (CrossPlatformInputManager.GetButtonDown ("Jump") && extraJumps > 0) {
rb.velocity = Vector2.up * jumpForce;
extraJumps--;
} else if (CrossPlatformInputManager.GetButtonDown ("Jump") && extraJumps == 0 && isGrounded == true) {
rb.velocity = Vector2.up * jumpForce;
}
}
void Flip()
{
facingRight = !facingRight;
transform.Rotate (0f, 180f, 0f);
}
public void knockBack()
{
//put knockback code here, since its called in the Hurt function in Health
rb.velocity = Quaternion.AngleAxis(-70, transform.right) * transform.forward * knockBackForce;
knockBackNow = true;
if (isGrounded == true) {
knockBackNow = false;
}
}
}
Your answer
Follow this Question
Related Questions
Workarounds for Higher Control When Using Physics2D? 0 Answers
Collision2D.relativeVelocity should be 0? 1 Answer
OnCollisionEnter2D and OnCollisionExit2D issue 1 Answer
trying to reverse 2D player's gravity after colliding with specific object 1 Answer
Physics2D.SetLayerCollisionMask delays before being applied 0 Answers