Ahlywog
October 27, 2014, 11:37pm
1
The subject sums it up nicely. I’m using a code example I found and it works most of the time.
void Update ()
{
if (renderer.isVisibleTo(Camera.main) == true)
{
isWrappingX = false;
isWrappingY = false;
//collider.enabled = true;
return;
}
if (isWrappingX == true && isWrappingY == true)
{
return;
}
var viewportPosition = Camera.main.WorldToViewportPoint (transform.position);
var newPosition = transform.position;
if (isWrappingX == false && (viewportPosition.x >= 1f || viewportPosition.x <= 0f))
{
newPosition.x = -newPosition.x;
isWrappingX = true;
//collider.enabled = false;
}
if (isWrappingY == false && viewportPosition.y >= 1f || viewportPosition.y <= 0f)
{
newPosition.y = -newPosition.y;
isWrappingY = true;
//collider.enabled = false;
}
transform.position = newPosition;
}
I don’t know what I’m doing wrong but eventually my asteroids are escaping. The only common factor I can find between them is isWrappingX and isWrappingY are both true when it happens. What am I missing and is there a better way I can be doing this?
Ahlywog
October 27, 2014, 11:38pm
2
Oh, and for clarification sake; “escaping” means eventually they stop wrapping and just float off into space.
Ahlywog
October 27, 2014, 11:42pm
3
And “isVisibleToo” comes from the following:
public static class RendererExtensions
{
public static bool isVisibleTo(this Renderer renderer, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes (camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
}
I’ve been playing with it since I created this thread and I’ve come up with some interesting observations.
Firstly, I changed the code:
void Update ()
{
var viewportPosition = Camera.main.WorldToViewportPoint (transform.position);
var newPosition = transform.position;
if (isWrappingX && viewportPosition.x <= 1f && viewportPosition.x >= 0f)
{
isWrappingX = false;
}
if (isWrappingY && viewportPosition.y <= 1f && viewportPosition.y >= 0f)
{
isWrappingY = false;
}
if (!isWrappingX && (viewportPosition.x > 1f || viewportPosition.x < 0f))
{
newPosition.x = -newPosition.x;
isWrappingX = true;
}
if (!isWrappingY && (viewportPosition.y > 1f || viewportPosition.y < 0f))
{
newPosition.y = -newPosition.y;
isWrappingY = true;
}
transform.position = newPosition;
}
I was still getting the same problems but I did notice something:
var viewportPosition = Camera.main.WorldToViewportPoint (transform.position);
The Viewport of Camera.main is smaller in Scene view than it is in Game view. So objects that are in bounds in one are out of bounds in the other. This is one reason my asteroids are escaping. Might be the only reason. Still playing with teh codez!
Tyche10
September 28, 2015, 2:41pm
5
Hi,
This is an old question but since it still has no answer, I thought I’d give one.
I finally solved this problem by using renderer.bounds and making sure that after wrapping at least one bound is visible again for the camera.
Hope this helps others who are struggling with the same problem.