Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Bigproblem01 · Mar 04, 2015 at 04:44 PM · bugscreendebugwrapwrapping

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bigproblem01 · Mar 04, 2015 at 07:37 PM 0
Share

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;
     }

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

black screen after the first play on android 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges