-
Notifications
You must be signed in to change notification settings - Fork 15
Screenwrapping
antfarmar edited this page Nov 14, 2015
·
8 revisions
ScreenWrapper.cs is the script component that screen wraps any GameObject it is attached to.
It is currently implemented as a slick hack using the OnBecameInvisible() Renderer message 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.
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.
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);
}