Howdy,
I have written this script to wrap objects around in space à la Asteroids, but in 3D.
It works fine (don’t have to worry about them wrapping to the exactly correct position, or having to have a duplicate of the object (one wrapping out, one wrapping in) etc, since it’s in 3D, and the boundary is invisible in world space).
However, my code definitely doesn’t follow the DRY principle.
I was wondering if anyone might help me with a function which would replace the repetitions.
it would really just need to take in two parameters. One for in which axis the boundary is being exceeded. And one for whether the boundary is being exceeded in the positive or the negative.
(The Portal() function just marks where the asteroid goes through the ‘boundary’ with a portal animation (on the gameObject ‘wrapWarp’))
public class PositionWrap : MonoBehaviour
{
Vector3 asteroidSize;
float asteroidDiameter;
public float boundaryWidth = 100;
public GameObject wrapWarp;
public GameObject wrapWarpClone;
void Start()
{
asteroidDiameter = transform.localScale.x;
asteroidSize = transform.localScale;
}
void Update()
{
Vector3 asteroidPos = transform.position;
if (transform.position.x < -boundaryWidth)
{
Portal(Vector3.right);
asteroidPos.x += boundaryWidth * 2 - asteroidDiameter;
}
if (transform.position.x > boundaryWidth)
{
Portal(Vector3.left);
asteroidPos.x -= boundaryWidth * 2 - asteroidDiameter;
}
if (transform.position.y < -boundaryWidth)
{
Portal(Vector3.up);
asteroidPos.y += boundaryWidth * 2 - asteroidDiameter;
}
if (transform.position.y > boundaryWidth)
{
Portal(Vector3.down);
asteroidPos.y -= boundaryWidth * 2 - asteroidDiameter;
}
if (transform.position.z < -boundaryWidth)
{
Portal(Vector3.forward);
asteroidPos.z += boundaryWidth * 2 - asteroidDiameter;
}
if (transform.position.z > boundaryWidth)
{
Portal(Vector3.back);
asteroidPos.z -= boundaryWidth * 2 - asteroidDiameter;
}
transform.position = new Vector3(asteroidPos.x, asteroidPos.y, asteroidPos.z);
}
void Portal (Vector3 facing)
{
wrapWarpClone = (GameObject) Instantiate(wrapWarp, transform.position, Quaternion.LookRotation(facing));
wrapWarpClone.transform.localScale = asteroidSize;
}
}