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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Jovy83 · Apr 07, 2015 at 01:37 AM · gameobjectpositionscreenoffscreen

How to re-position game object to the left side of the screen when it goes off the right side of the screen?

I am currently working on a portrait game and I'm trying to achieve something like the offscreen movement logic of the games Doodle Jump or Radical.

How do I re-position a game object to the left side of the screen when it goes off the right side of the screen and vice versa without being dependent of the screen size/aspect ratio?

Here's a snippet of code that I have right now:

 void CheckBounds()
     {
         if (this.transform.position.x < -3.2f)
         {
             this.transform.position = new Vector3(3.2f, this.transform.position.y, this.transform.position.z);
         }
 
         if (this.transform.position.x > 3.2f)
         {
             this.transform.position = new Vector3(-3.2f, this.transform.position.y, this.transform.position.z);
         }
     }

 void MovePlayer()
 {
             if (isFacingLeft)
         {
             this.transform.position -= new Vector3(speed * Time.deltaTime, 0, 0);
         }
         else
         {
             this.transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
         }
 }

Now this works but it's a dirty solution. As you can see I hard coded the bounds when the object goes offscreen which is +/- 3.2f.

This works perfectly on 16:9 aspect ratio devices but not on other aspect ratio devices.

How do I detect the screen edges to make this work regardless of the screen aspect ratio?

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 _Gkxd · Apr 07, 2015 at 02:18 AM 0
Share

You can use WorldToScreenPoint to check if an object is leaving the screen. Then you can wrap the object's position around in screen coordinates and use ScreenToWorldPoint to convert that point back to world coordinates.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by LMan · Apr 07, 2015 at 02:30 AM

ViewportToWorldPoint might be quite simple. Convert 0 and 1 from viewport to world space and store the values of the axis you want to wrap in a variable in Start(). Then run a check every frame to see if the object's position is equal to or outside those points. if it is, set the appropriate axis to the appropriate point that you already saved. I do hope that makes sense. Good luck!

Comment
Add comment · Show 1 · Share
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 Jovy83 · Apr 07, 2015 at 04:42 AM 0
Share

Thanks for the help! I got it working now.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Position GameObject to Bottom Left Corner 2 Answers

Set game object position relative to the Camera 1 Answer

How to set position of 4 gameobjects at the bottom of the screen? 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers

Camera rotation around player while following. 6 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