Simply creating a box

For some reason I just can’t figure out how to do this but basically I’m trying to make a simple box that the player can be placed in too but can’t escape due to a collider/border around the box with the ability to be able to resize the box in code while also changing the size/placement of the collider/border. If you want to see an example of what I mean then look at battles in undertale as I’m basically trying to recreate the same thing within unity. Skip to 1:35 of the video:


I’m referring to the simple black box with a white border around it that can be resized in code. I basically want to recreate that in unity still being able to resize it in code and have a collider of some kind around the white border so the player can’t escape the box but I’m not sure how to do it. Should I make a sprite of the box? or use GUI to create the box and be able to resize, have no idea…

I would love if someone would be able to help me out?

Thanks

@Codelyoko363

  • 1.Put the player sprite in the middle of where you want your bullet box to be with a layer order of 2 or higher.
  • 2.right click on your project window and create a square sprite and name it "Black".
  • 3. repeat step 2 but name it "White".
  • 4. place the Black sprite where the player sprite is, then set the layer order to -1 or 0 then rescale it to where you'd like
  • 5. Place White sprite on each edge of the Black sprite then rescale it to wear you'd like it.
  • 6. Add a RigidBody2D to each White sprite. Then set the mode to Kinematic.
  • 7. Add a BoxCollider2D to each White sprite.
  • Create a new C# Script, name it PlayerMovement. Then paste the following code:
  • using UnityEngine;
    // If this is wrong, sorry I was at school instead of at my computer.
    public class PlayerMovement: MonoBehaviour {
    public Transform Player;
    void Update() {
    if(Input.GetKey(KeyCode.LeftArrow)) {
    if(Input.GetKey(KeyCode.UpArrow)) {
     Player.position = new Vector3(Player.position.x - 1, Player.position.y + 1, Player.position.z);
    }
    else if(Input.GetKey(KeyCode.DownArrow)) {
     Player.position = new Vector3(Player.position.x - 1, Player.position.y - 1, Player.position.z);
    }
    
     Player.position = new Vector3(Player.position.x - 1, Player.position.y, Player.position.z);
    }
    else if(Input.GetKey(KeyCode.RIghtArrow)) {
    if(Input.GetKey(KeyCode.UpArrow)) {
     Player.position = new Vector3(Player.position.x + 1, Player.position.y + 1, Player.position.z);
    }
    else if(Input.GetKey(KeyCode.DownArrow)) {
     Player.position = new Vector3(Player.position.x + 1, Player.position.y - 1, Player.position.z);
    }
    
     Player.position = new Vector3(Player.position.x + 1, Player.position.y, Player.position.z);
    }
    else if(Input.GetKey(KeyCode.UpArrow)) {
     Player.position = new Vector3(Player.position.x, Player.position.y + 1, Player.position.z);
    }
    else if(Input.GetKey(KeyCode.DownArrow)) {
     Player.position = new Vector3(Player.position.x, Player.position.y, - 1, Player.position.z);
    }
    }
    }
    

    If this is wrong, sorry I was at school instead of at my computer.

  • Assign the player sprite to the Player varible and add a BoxCollider2D to your Player sprite!
  • Now your done!