Need help to understand a problem im having with colliders

Hello!

Im a programmer new to unity, trying to learn game development and build my first game.
On my 2D game, i want to have a circle sprite floating around and when the player collides with it, it should send the player to the opposite direction, on the Y and X coordinates, with a fixed set velocity.
i came with the following solution:

have the circle sprite with a trigger collider, with the following code when it collides with the player:

        float xVelocity = Mathf.Abs(circle.position.x - transform.position.x) * 14;
        float yVelocity = Mathf.Abs(circle.position.y - transform.position.y) * 14;

        if (circle.position.x > this.transform.position.x)
            xVelocity = xVelocity * -1;

        if (circle.position.y > this.transform.position.y)
            yVelocity = yVelocity * -1;

        body.velocity = new Vector2(xVelocity, yVelocity);

so with this i get the difference of the y and x position from the player transform and the circle transform, multiply it to the fixed speed i want to give the player.
its working fine but there is a consistency problem that i just figured out why, but dont know how i can improve it.
the problem is that depending on the velocity of the player going in the direction of the circle, it gets more inside of the circle collider, reducing the distance between the x or y coordinates, wich results in less speed applied on my code. i want the result to be the same if the player hit the same surface point of the circle.
theres a image that i drew trying to explain:

it there a way to make it more consistent? or is there another better way to do all this?

Place this inside a script on the circle:

	void OnTriggerEnter2D(Collider2D c)
	{
		c.attachedRigidbody.velocity=(c.transform.position-transform.position).normalized*14;
	}

It’s be simpler to get the direction to the circle (or the reverse), normalise that direction so it always has a magnitude of 1, and use that to push the player.

Definitely learn to use Unity’s core built in types such as Vector2/Vector3. There’s lots of good information in the manual on it: Unity - Manual: Moving objects with vectors

Or I guess Zulo’s unreadable code also works.

still the same problem. maybe iam not understanding it correctly? to test i put the player above and a little to the side of the circle, start the scene and let it fall towards the circle, then do it again but increase the player altitude. im expecting it to bounce and fall on the ground at the same position, because it hit the same surface point of the circle, but its not happening, the landing point varies.

thanks, im gonna have a look at it.

If it hits exactly the same point each time, then it should push in the same direction and land at the same point. This is due to their code normalizing the direction.

But if you change the point at which the box falls from, even if only vertically, then it’s not going to hit the same point every time. Depending on the velocity it will likely hit later or earlier based on where you positioned it compared to your reference starting position.

Unity’s 2d physics are meant for runtime performance, not high-precision and determinism.