Need help correcting player knock back by enemy script

What do i need to do further to make the player actually bounce back from colliding with the enemy?

I made the codes properly with a debug message that pops up when their colliders touch but theres nothing happening where i can actually see the player being knocked back. i was trying to achieve that same Castlevania or even Ninja gaiden nes still knock back hit by enemies.

Player collision script:
```csharp
**public class PlayerCollision : MonoBehaviour
{

public float knockbackForce = 10f;
public Rigidbody2D rb;

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Enemy")
    {
        Vector2 knockbackDirection = (rb.position - (Vector2)collision.transform.position).normalized;
        rb.AddForce(knockbackDirection * knockbackForce, ForceMode2D.Impulse);
        Debug.Log("Player hit by enemy");
    }
  
}

}**
** **And heres the enemy collision script:** **csharp
**public class EnemyCollision : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == “Player”)
{
Debug.Log(“Enemy Hurt player!”);
}
}

}**
```
All i need to know further is what am i missing in code or what steps i need to make further for the actual effect to trigger instead of just the debug message
Thank you

Hmm, that looks reasonable to me.

I would double-check that the force vector makes sense by drawing it:

Debug.DrawRay(rb.position, knockbackDirection, Color.white, 1f);

That should display a line for one second, starting at the player’s position and pointing in the direction of the force. You’ll need to have gizmos toggled on to see them (they’re off by default in Play Mode).

Thanks

Ok i can see the line appear which is something for a start but still no actual knock back. i revised the code with a snippet i saved a while back. had to find it in my notes so now i can get bounce on the enemy head for a hit. which is ok for now but if can get push back from a collide horizontal and that, that would be better.

Anyway, for your curiosity this is what the codes looks like now in the player script:

bool hitSideRight;

    public float knockbackForce = 5f;
    public Rigidbody2D rb;



    public void HitSide(bool rightSide)//
    {
        // determines the push direction of the hit animation
        hitSideRight = rightSide;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            Vector2 knockbackDirection = (rb.position - (Vector2)collision.transform.position).normalized;
            rb.AddForce(knockbackDirection * knockbackForce, ForceMode2D.Impulse);
            Debug.DrawRay(rb.position, knockbackDirection, Color.white, 1f);
        }
    }

The enemy collision script revised:

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            PlayerCollision player = collision.gameObject.GetComponent<PlayerCollision>();
            if (player != null)
            {
                Vector2 knockbackDirection = (player.rb.position - (Vector2)collision.transform.position).normalized;
                player.rb.AddForce(knockbackDirection * player.knockbackForce, ForceMode2D.Impulse);
                player.HitSide(transform.position.x > player.transform.position.x);
            }
            Debug.Log("Player hit by enemy");
        }
    }

for the record the right side Bool method i tried to emulate from what i saw in an old megaman get hit tutorial video. where walking into or jumping on top of an enemy you get a bounce knock back… my poor attempt

Heres a visual test of what im seeing

How are you driving the player’s rigidbody?

If you’re just setting it’s .velocity property each FixedUpdate then applying forces isn’t going to do anything.

it has a separate script for Playermovement not shown there with rigidbody in it if thats what youre wondering. thats how im driving the player. I assume i did that wrong? was the collision code supposed to be all in one code with the player movement is what you mean?

No what saying is, what methods are you using to move the rigidbody in your player controller? Are you adding forces? Are you mutating it’s .velocity property? Are using Rigidbody.MovePosition()?

Not all the various methods are compatible with one another.

I was trying to add forces just between the two scripts

like this portion

player.rb.AddForce(knockbackDirection * player.knockbackForce, ForceMode2D.Impulse);

Im not keen on what methods are compatible with the other yet entirely

i have nothing in fixed update between those two scripts. any suggestions?

Two posts and you’ve yet to answer my question.

I’ll only ask one more time: how are moving your player’s rigidbody in your player controller?

then dont bother because at this point youre confusing me now.

I genuinely don’t understand what’s confusing. I just want to know how you move the player’s rigidbody with your player controller script.

Like I said, some methods of moving rigid bodies aren’t compatible with others. That’s what I’m trying to suss out.

If you can’t answer this question you likely won’t find a solution.

If you set the velocity of the player’s rigidbody every frame, then adding a force will not do anything, since you will instantly overwrite the rigidbody’s old velocity with the player’s input.

Youre confusing me because i dont know if youre talking about what am i trying to use in method to get the player’s Rigidbody to move while hit during a collide OR are you talking literally how my player is moving back and forth to walk which i have in my player movement script so that it moves or jumps. If you mean that then its Velocity. If thats not what it is you are asking me then i dont know

for example with jump my Rigibody with it using Velocity is

 if (Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);

        }

Other than that i went with the addforce method from the tutorials ive been trying to look at but its like half working

yes im more so using Velocity (i guess) from My other script not shown which is the player movement script i made. there i more so use Velocity. The player collision and player movement scripts are separate. AddForce i honestly only tried to use with my collision scripts. so ultimately it sounds like you both are saying remove that addforce there and instead use Velocity?

This. Your primary method of controlling your player (aka, your player controller) affects how other sources will have to be designed in order to influence it.

Pretty much, as I said right at the beginning:

If you’re driving it through the .velocity property, then everything else is going to have to do the same.

You’re effectively in DIY territory as you’re overriding/bypassing most of the rest of the rigidbody API.

Post the other script.

The others are trying to determine whether or not you are doing something like… rigidbody.velocity = something; …in the script which you’re not showing us.

Because if you are, then that is overriding anything that you do in your knockback script.

Thats what i was trying to tell them. its in my other script for the player movement to move back n forth. do you mean this?

void FixedUpdate()
{
movement = new Vector2(mX * Speed, rb.velocity.y);

rb.velocity = movement; <<<<<<-------- Here

}

in my collision script i was only using addforce because thats what i thought the proper method was from what i was following for situations like that. thats where the confusion for me was at. whether they meant Rigidbody moved doing a hit or during gamepad or keyboard moving the character etc

Ok now i understand.

I wasnt sure exactly what you were looking for but thats why i said before i may have something like that in the playermovement script i created separate which is different. I think this is the example you more so wanted to see (?)

void FixedUpdate()
    {
        movement = new Vector2(mX * Speed, rb.velocity.y);
    

       rb.velocity = movement;

    

    }

In any case, this is most likely your path forward:

This is potentially going to involve expanding your player controller API.

Explain further what you mean, if you dont mind. is that a major task er somethin? Whats API?