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?
