-
Notifications
You must be signed in to change notification settings - Fork 15
Screenwrapping
ScreenWrapper.cs
is the initially committed script component that screen wraps any GameObject it is attached to.
It is currently implemented as a slick hack using the OnBecameInvisible()
message from Renderer
to determine when the object leaves the viewport, but it has issues:
- An object is considered visible when it needs to be rendered in the scene.
- So it might not be actually visible by any camera, but still needs to be rendered for shadows for example.
- Also, when running in the editor, the scene view cameras will also cause this function to be called.
- Rare edge cases where an object is caught in floating-point limbo.
- Objects have to fully disappear to wrap. No continuity.
- Have to create a another "ghost" object to have fake continuity.
-
Camera.main
throws errors on application quit.
Given these issues though, if you're not using shadows and have your scene view off or zoomed in slightly more than the game view when playtesting, you're pretty much good to go with little code.
public void OnBecameInvisible()
{
Vector3 viewportPosition = Camera.main.WorldToViewportPoint(transform.position);
Vector3 newPosition = transform.position;
if (viewportPosition.x < 0 || viewportPosition.x > 1)
newPosition.x = -newPosition.x;
if (viewportPosition.y < 0 || viewportPosition.y > 1)
newPosition.y = -newPosition.y;
// Can deactivate/disable the gameObject/components temporarily when we wrap/teleport.
// Prevents funky behaviour. (eg. particle systems based on distance emission)
//gameObject.SetActive(false);
transform.position = newPosition;
//gameObject.SetActive(true);
}
Implementing screen wrapping based of off "visibilty" functions in Unity's API is not the proper way to get a stable, robust, and bug-free algorithm. It would be preferable to base it off of discrete bounds. For this reason, the algorithm has been redesigned to strictly use screen bounds as well as object bounds to determine "invisibility".
See an implementation: Screenwrapper.cs