- Home /
screen wrap bug, can't identify the issue
Hi guys,
I implemented this screen wrap in my game. my object changes directions randomly and sometime when it goes off the screen, it doesn't come back. When I look at the scene view it's somewhere far away at some random location. I can't track the scene view at the same time to see what happens to objects location exactly when the bug happens because "OnBecameInvisible" won't work if the object renderer is visible in the scene view.
At first I thought it's my fault but then I saw the demo on the link I mentioned and it has the same bug. Also people mention it in the comment section so it's definitely there. As the last comment says, it happens if you change the direction just when you become invisible.
My problem is that I just can't pinpoint what's wrong with this code and thus am unable to fix it...
this is the code I'm using (mentioned in the link as well)
void OnBecameInvisible() //if screenWrap is true, then the wrapping happens
{
if(screenWrap)
{
newPosition = transform.position;
if(screenDimensions.x > 1f || screenDimensions.x < 0f)
newPosition.x = -newPosition.x;
if(screenDimensions.y > 1f || screenDimensions.y < 0f)
newPosition.y = -newPosition.y;
transform.position = newPosition;
InvokeRepeating ("OffScreenTime", 0, Time.deltaTime);
}
}
Any ideas what's going on?
Thanks
I think I found the issue. I'll post it here for anyone having the similar problem
I removed the visibility factor altogether to observe the bug in the scene view. What happens is that sometimes the object keeps shifting positions from newPos to -newPos and I think this is happening because sometimes screenDimensions (which is Camera.main.WorldToViewportPoint(transform.position)
) becomes way more than 1 and when the shift happens it's still satisfies the if
statements and just keeps shifting forever... sometimes it breaks out of this loop but it may take very long.
If I were to do execute this script on everyframe ins$$anonymous$$d of with OnBecameInvisible
, it'd be even less reliable. However OnBecameInvisible
has a loophole for the bug as well. If the object decides to change direction right after the position shift, it'll just go in the other direction (further offscreen)...
So this is how I solved it
void Wrap() //if screenWrap is true, then the wrapping happens
{
newPosition = transform.position;
if(screenDimensions.x > 1.1f || screenDimensions.x < -0.1f)
{
if(screenDimensions.x > 1.1f)
newPosition = Camera.main.ViewportToWorldPoint(new Vector3(1.05f, screenDimensions.y, screenDimensions.z));
if(screenDimensions.x < -0.1f)
newPosition = Camera.main.ViewportToWorldPoint(new Vector3(-0.05f, screenDimensions.y, screenDimensions.z));
newPosition.x = -newPosition.x;
}
if(screenDimensions.y > 1.1f || screenDimensions.y < -0.1f)
{
if(screenDimensions.y > 1.1f)
newPosition = Camera.main.ViewportToWorldPoint(new Vector3(screenDimensions.x, 1.05f, screenDimensions.z));
if(screenDimensions.y < -0.1f)
newPosition = Camera.main.ViewportToWorldPoint(new Vector3(screenDimensions.x, -0.05f, screenDimensions.z));
newPosition.y = -newPosition.y;
}
transform.position = newPosition;
}
Your answer
Follow this Question
Related Questions
Turned screen 1 Answer
Monodevelop deleted my hierarchy/scene 2 Answers
Debugging opens a new tab :( 1 Answer
Keep getting AudioTrack messages when I use Android logcat with my unity app. 0 Answers