Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity sprite border help

Discussion in '2D' started by macha77, Jul 11, 2023.

  1. macha77

    macha77

    Joined:
    Aug 12, 2021
    Posts:
    54
    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?
     
  2. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    163
    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)
     
  3. macha77

    macha77

    Joined:
    Aug 12, 2021
    Posts:
    54
    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.
     
  4. macha77

    macha77

    Joined:
    Aug 12, 2021
    Posts:
    54
    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.
     
  5. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    163
    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.
     
  6. macha77

    macha77

    Joined:
    Aug 12, 2021
    Posts:
    54
    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?
     
  7. macha77

    macha77

    Joined:
    Aug 12, 2021
    Posts:
    54
    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?
     
  8. MushyAvocado

    MushyAvocado

    Joined:
    Oct 16, 2021
    Posts:
    20
    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)

    Code (CSharp):
    1. var clampedX = Mathf.Clamp(player.transform.position.x, -borderWidth / 2f, borderWidth / 2f);
    2. var clampedY = Mathf.Clamp(player.transform.position.y, -borderHeight / 2f, borderHeight / 2f);
    3. player.transform.position = new Vector3(clampedX, clampedY, player.transform.z);
    You might also be interested in BoundsInt.ClampToBorder for more flexibility.