I’m currently developing a top down 2D shooter in the style of asteroids. I need to be able to make maps the player traverses in a linear fashion going from one encounter to another. So I need to make areas where the player can fly, and the player can’t fly, and I’m not sure of the best way to do this in 2D with Unity. My first thought was to lay out out a collection of Colliders, making a more complex map out of simple shapes (Spheres, Rectangles, etc.) and when the player is no longer within the boundaries of any Collider then we know he is out of bounds and should be able to fly no further in that direction. So in psuedo code it might look something like this.
ArrayList<Collider> boundaries;
void OnTriggerEnter(Collider other)
{
if(other.tag == "boundary")
{
if(!boundaries.contains(other)
{
boundaries.add(other);
}
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "boundary")
{
if(boundaries.contains(other)
{
boundaries.remove(other);
}
}
}
void Update()
{
if(boundaries.size() != 0)
{
//Move player forward in current direction
if(boundaries.size() == 0)
//Move player back in bounds to previous location.
}
}
This code will keep the player in bounds. However, I would like the player to sort of slide against the boundary walls when he reaches them, and with this code, he’ll just stop dead in his tracks. Kind of getting stuck against the wall.
Any suggestions?