I have a level in which the player (kinematic - rigidbody2d, box collider) moves around the screen, avoiding enemies and whatnot. I want to have a boundary around the level so the player doesn’t disappear through the sides of the scene, I’d also like to have obstacles in the scene just to add difficulty.
I’m having problems with this because my player is kinematic, and the player floats through colliders. The wall is a static rigidbody2d with a box collider Is there any way I can create a boundary in order to ‘fake’ the walls? Or is there some way I can force a kinematic gameobject to collide?
Thanks in advance guys!

I have 2 possible ideas, not sure which one you should prefer or which works “better”:
-
Use the box collider as isTrigger. And then, when the player is inside the trigger just avoid moving your player.
-
It looks like your Gamefield is always rectangle. So you actually know the borders and can use the coordinates of it, because of that a movememt script of this kind should work:
float xMin = -5f;
float xMax = 5f;
float yMin = -7f;
float yMax = 10f;
void Move(){
Vector3 tmp = transform.position;
if(tmp.x < xMin){
tmp.x = xMin;
}
else if(tmp.x > xMax){
tmp.x = xMax;
}
else if(tmp.y < yMin){
tmp.y = yMin;
}
else if(tmp.y > yMax){
tmp.y = yMax;
}
transform.position = tmp;
}
The values are just placeholders. Set your correct values for the min and max variables to the positions of your borders.
Create a empty object, assign it a box collider, adjust, and box in the outer walls/perimeter.
@nerf_herder42
sorry this way is better
//Keeps the player inbounds
public float xRange
if (transform.position.x < -xRange) {
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
}
if (transform.position.x > xRange) {
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
}