- Home /
Top Down 2D: How to make a game perimeter that keeps objects inside
I'm attempting to make a (very simple to begin with) top-down game in 2D wherein the player (using arrow keys) is able to move around an enclosed polygonal area of the game. I'm having trouble ensuring that all parts of the area's perimeter (made of a polygonal collider) are impenetrable (areas near corners tend not to be for some reason). Code below:
public float speed = 40f;
public bool heTouchedTheButt = false; //test whether the player is colliding with the perimeter
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.name == "Perimeter")
heTouchedTheButt = true;
}
void OnCollisionExit2D(Collision2D coll) {
if (coll.gameObject.name == "Perimeter")
heTouchedTheButt = false;
}
void FixedUpdate (){
if (!heTouchedTheButt) {
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += Vector3.down * speed * Time.deltaTime;
}
} else if (heTouchedTheButt) {
if (Input.GetKey (KeyCode.RightArrow)) {
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey (KeyCode.UpArrow)) {
transform.position += Vector3.down * speed * Time.deltaTime;
}
}
}
I figured setting the keys to be inversed when the player is in contact with the perimeter would allow them to come out of contact, but this appears to work only some of the time. Other times, the controls get stuck in the inverse and others allow the player to go right past the boundary as if it weren't there. I feel like there's a more efficient way to set up this boundary, and I appreciate any insight on how to make this easier.
does the player have a collider? I'm no 100% sure what you're saying, you mean two collider can pass through each other? because that shouldnt normally happen.
Answer by Jay-Pickle · Nov 19, 2015 at 01:08 PM
Hi cecilfuel95,
You are probably encountering this problem because you are assigning transform.position. This is actually more like teleporting rather than moving. So if transform.position += Vector3.down * speed * Time.deltaTime
happens to be on the other side of the collider, Unity dosent have a problem teleporting it to that position.
If I was you, I would give the game object your moving a Rigidbody or Rigidbody2D. And then do something like:
public float speed;
private Rigidbody2D rigidbody;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if(Input.GetKey(KeyCode.RightArrow))
{
rigidbody.velocity = (Vector2.right * speed);
}
}
Since this involves physics, use FixedUpdate(). You don't have to worry about Time.deltaTime here. Let me know if that works out for you.
Thanks, @$$anonymous$$ Pickle! Your insight was very helpful and I was able to correct my problem easily. If you have any tips on my next problem, I would appreciate the help. Now I'm working on making the player teleport between scenes to the same position (the different scenes representing different storeys in a building and the spot of teleportation representing stairs from one floor to the next). The "stairs" act as a trigger which loads the scene the player intends to go to, but I haven't been able to make it so that when the stairs are triggered and the next scene is loaded, that the player appears in the same position they were in when they triggered the stairs. Ins$$anonymous$$d, the player goes to (0,0) by default. Should I get the player's position first thing when the stairs are triggered, then load scene, then apply the saved position to the player? Thanks!
Hi cenifuel95,
I can't think of anything off the top of my head, I suggest posting a new thread with that question.
Thanks, @$$anonymous$$ Pickle! Your insight was very helpful and I was able to correct my problem easily. If you have any tips on my next problem, I would appreciate the help. Now I'm working on making the player teleport between scenes to the same position (the different scenes representing different storeys in a building and the spot of teleportation representing stairs from one floor to the next). The "stairs" act as a trigger which loads the scene the player intends to go to, but I haven't been able to make it so that when the stairs are triggered and the next scene is loaded, that the player appears in the same position they were in when they triggered the stairs. Ins$$anonymous$$d, the player goes to (0,0) by default. Should I get the player's position first thing when the stairs are triggered, then load scene, then apply the saved position to the player? Thanks