- Home /
Can I create boundaries around my level without using box colliders?
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!
Answer by BBIT-SOLUTIONS · Apr 06, 2020 at 01:09 AM
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.
Thank you so much!! Your second suggestion worked perfectly for my outer walls. I tried the first suggestion for obstacles in the scene but found my player got stuck when I set the speed to 0, assu$$anonymous$$g because I have collision detection set to continuous, don't suppose you havee any other excellent suggestions for this? XD
Oh you are right. Did not thought about the inner obstacles.
$$anonymous$$aybe a similar mix between the both approaches could work. Just a little Pseudo-code to describe the idea (you should assign that script then to each inner obstacle and mark their collider as trigger):
float my$$anonymous$$inX, my$$anonymous$$axX, my$$anonymous$$inY, my$$anonymous$$axY;
void Awake(){
my$$anonymous$$inX = transform.position - 0.5f; //use the (half) width of your element ins$$anonymous$$d of 0.5
my$$anonymous$$axX = transform.position + 0.5f;//same here
/* same for Y and height of obstacle... */
}
void OnTriggerEnter2D(Collider2D other){
if(other == player){
//use here the same approach like for the outer border
tmp = other.transform.position;
if(tmp.x > my$$anonymous$$inX){
tmp.x = my$$anonymous$$inX;
}
/*...same for the other for borders*/
other.transform.position = tmp;
}
}
Or maybe ins$$anonymous$$d of using OnTriggerEnter2D() in this case also OnTriggerStay2D() could be more useful, not sure...
Answer by warthos3399 · Apr 05, 2020 at 02:23 AM
Create a empty object, assign it a box collider, adjust, and box in the outer walls/perimeter.
Thanks for the reply, I can't use colliders as my player object must be kinematic, which doesn't allow for typical collisions. Is there a way i can use the OnTriggerEnter2D() method to fake collision?
Answer by Vipertex13 · Apr 05, 2020 at 05:15 AM
@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); }
Thanks for the reply, I tried your suggestion, but I'm having the same issue with the kinematic player just floating through the walls. Is there something different I can do with a mesh collider to stop this?
Here i edited the post check it again $$anonymous$$ost simple and easy code
This only xAxis you can also make it yAxis too
This will work better so that you can just pinpoint the boundary location on X and Y Axis
Can be done more simply by clamping.
transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, 0, x$$anonymous$$ax), $$anonymous$$athf.Clamp(transform.position.y, 0, y$$anonymous$$ax), transform.position.z);