I am trying to add force in the opposite direction of my colliding object (in this case the player). I’ve scrolled through a dozen of answers but they seem to give me no results. AddForce simply refuses to move the object. (2D space)
This is how I move my enemy which I want to push back on collision. To clarify the object is Dynamic, Mass is 1 and gravity scale is 0:
public void Movement_Follow(GameObject player, float Speed)
{
Vector3 playerPosition = player.transform.position;
transform.position = Vector3.MoveTowards(transform.position, playerPosition, Speed);
}
However, even with the moving commented out and simply spawning them at a certain position and then running into them still gives me nothing.
And this is how I try to push it back when it bumps in the player (opposite direction):
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.name == "Player")
{
health.Damage(5);
float force = 50;
//rb is the components RigidBody2D initialized in Start()
rb.AddForce(-collision.gameObject.GetComponent<Rigidbody2D>().velocity.normalized * force, ForceMode2D.Impulse);
}
}
I considered that maybe I don’t have enough force applied, but even values of 500 000 produce no effect. Different methods of adding force also give me no results.