Unity sprite border help

sometimes the enemy sprites push the player sprite outside of the border and the player sprite can’t get back inside the border once the player sprite is pushed outside of the border. how do i make it so the player sprite will never get pushed outside of the border by enemy sprites?

If you are using physics - activate continuous collision detection. Also you can write a script that ensures the player will always stay within the level bounds (just define a rect border and check the x,y position + width/height against the border rect)

Hi. How do I activate that? I don’t see it on the player or on the border in inspector. How do define a rect border? I have a regular 4 sided colored border. But it doesn’t have x, y positions on it. It has 4 box collider 2d things on it. They don’t have an x or y on them.

Update: continuous collision is checked but it doesn’t stop enemy sprites from still pushing the player sprite outside of the border. The border for some reason isn’t strong enough to stop the push out.

You could try to make the border box collider larger to avoid the pushout.
If that doesn’t help you will need a script i.e on each player or on the border and check if any player is outside the bounds.

It didn’t work. I just tried it. I have enemy snakes, spiders, and chasers moving at the fast speed of 16. my player is moving at the fast speed of 17. and i have an attacker sprite that follows the player around at the fast speed of 16. it seems the force of the attacker sprite is pushing out the player sprite. because when i play without the attacker, the other fast moving sprites don’t push out player with combined forced. the attacker sprite seems to put it over the edge. but i have the attacker at a fast speed too for more challenging gameplay. you know what i mean? so if the problem is the attack sprite and not the border itself, how do i fix it?

It didn’t work. I just tried it. I have enemy snakes, spiders, and chasers moving at the fast speed of 16. my player is moving at the fast speed of 17. and i have an attacker sprite that follows the player around at the fast speed of 16. it seems the force of the attacker sprite is pushing out the player sprite. because when i play without the attacker, the other fast moving sprites don’t push out player with combined forced. the attacker sprite seems to put it over the edge. but i have the attacker at a fast speed too for more challenging gameplay. you know what i mean? so if the problem is the attack sprite and not the border itself, how do i fix it?

If your border is square, you can just constrain the player transform or rigibody position to be within the bounds. For instance, you can run this code in Update to constrain the player’s position. This snippet assumes that the border is centered on (0, 0)

var clampedX = Mathf.Clamp(player.transform.position.x, -borderWidth / 2f, borderWidth / 2f);
var clampedY = Mathf.Clamp(player.transform.position.y, -borderHeight / 2f, borderHeight / 2f);
player.transform.position = new Vector3(clampedX, clampedY, player.transform.z);

You might also be interested in BoundsInt.ClampToBorder for more flexibility.