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: https://www.youtube.com/watch?v=6RL6JcEHw2U
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
Answer by BlockFade · Feb 06, 2017 at 06:56 PM
- 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.
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.
Now your done!
Your answer
![](https://koobas.hobune.stream/wayback/20220612072619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to instantiate (MonoBehaviour) script multiple times and call void? 1 Answer
RTS Click Drag Selection box not working with GUI canvas 1 Answer
How to add text to a box ? 2 Answers
Mario Kart style power ups 0 Answers
Box cast issue 0 Answers