I have a fairly basic stage set up where the players are spawned on the opposite side around natural zero of the world - they end up in positive / negative positions. I would like to clamp them so that neither can leave the 2D playing field. At current, I have that accomplished by using this snippet:
Vector3 SpawnClamp = _transform.position;
// Positive
if (PlayerIDNumber == 0)
{
SpawnClamp.x = Mathf.Clamp(SpawnClamp.x, SpawnPosition.x, -SpawnPosition.x);
}
// Negative
if (PlayerIDNumber == 1)
{
SpawnClamp.x = Mathf.Clamp(SpawnClamp.x, -SpawnPosition.x, SpawnPosition.x);
}
However, it feels like there must be a simpler way to do this without repeating the line twice. I would rather reduce it down to one or two lines of code - if it is at all possible. Can that be done?