- Home /
Question by
shubhamishra746 · Aug 01, 2020 at 08:28 PM ·
2d game2d-physics2d-gameplay2d platformer
How can I stop hurt animation and bounce the player when hit by an enemy?
[SerializeField] float Bounce = 1000f;
//Enemy destroy and player hit
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "Enemy")
{
//Enemy is the other script for inheriting enemy death and player reward
Enemy enemy = other.gameObject.GetComponent<Enemy>();
if (rb.velocity.y < 0 && isGrounded == false)
{
//Destroying the enemy
enemy.JumpedOn();
rb.velocity = Vector2.up * jumpForce;
}
else if (other.gameObject.transform.position.x > transform.position.x)
{
//Enemy is to my right therefore I should get damage and bounce left
animator.SetBool("Hurt", true);
rb.velocity = new Vector2(-Bounce, rb.velocity.x);
Debug.Log("Hurt Left");
}
else if (other.gameObject.transform.position.x < transform.position.x)
{
//Enemy is to my left therefore I should get damage and bounce right
animator.SetBool("Hurt", true);
rb.velocity = new Vector2(Bounce, rb.velocity.x);
Debug.Log("Hurt Right");
}
else
{
animator.SetBool("Hurt", false);
}
}
}
Comment