I am pretty new to Unity... watched quite a few of Brackey's and other YouTube videos along with a lot of research. I have been trying to add an impulse to my player so that after it comes in contact with an enemy my player will be launched backwards. I have tried setting the horizontal speed to zero for a short time as the character impulse takes effect... Still not working. The 2D rigid body has a mass of 100, a gravity scale of 10, and continuous collision detection. Plz hlp...
Further Details, in case it helps:
It is a 2D mobile game that I'm building on Unity version 2021.3.18f1
I have added a virtual joystick that converts the input into a normalized vector3 InputDirection.
Code has been written in CSharp.
private void OnCollisionEnter2D(Collision2D col) { if (col.collider.tag == "Ground") { canJump = true; } if (col.collider.tag == "Slime") { EnemyCollision(1000000, 15, 0.5f); } if (col.collider.tag == "Crocodilian") { EnemyCollision(10000000, 25, 1.0f); } } private void EnemyCollision(int hitForce, int damage, float time) { TakeDamage(damage); speed=0; if(facingDirection != Vector3.right){ rb.AddForce(Vector2.right * hitForce, ForceMode2D.Impulse); } else { rb.AddForce(Vector2.left * hitForce, ForceMode2D.Impulse); } StartCoroutine(EnableMovementAfterDelay(time)); } private IEnumerator EnableMovementAfterDelay(float delay) { yield return new WaitForSeconds(delay); speed = 80f; //speed is multiplied by the InputDirection.x vector inside Update(). So, by setting it to 0, I am hoping it stops movement. // Looks like this: rb.velocity = new Vector2(horizontalInput * speed, rb.velocity.y); }