Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 bracciata · Aug 10, 2015 at 02:37 PM · android2dscreenphonetablet

Random X posistion between left and right edges of screen 2D

My goal is to have a app with characters running at you and you dodging but I want it to be universally good for tablets and phones. Preferably C# but java script is fine. What i want is having the character spawn at random position between the left edge of the screen and the right.

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 DwaynePritchett · Aug 10, 2015 at 02:46 PM 1
Share

You will probably need to be way more specific. But, you can take the Screen.Width and multiply it by a RandomRange(0,1). And, that would give you a random X/width value of the screen, that is catered to all resolutions/platforms. Look at the $$anonymous$$athClass, because I think there is a Random01() method that is specifically, 0-1.

Hope this helps.

-Dwayne Pritchett

1 Reply

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

Answer by whaleinthesea · Aug 10, 2015 at 03:50 PM

 vector3 v3Left = new Vector3(-0.15f, .5f, 10); //10 is the distance from the camera
 v3Left = Camera.main.ViewportToWorldPoint(v3Left);
 
 vector3 v3Right = new Vector3(Screen.width,0,0);
 v3Right = Camera.main.ScreenToViewportPoint(v3Right);
 v3Right = new Vector3(v3Right.x, .5f, 10);
 v3Right = Camera.main.ViewportToWorldPoint(v3Right);
 


For me, this worked v3Left.x is the left edge of the screen and v3Right.x the right edge.

Comment
Add comment · Show 2 · 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 bracciata · Aug 10, 2015 at 08:44 PM 0
Share

.15f how does that work to to detect a posistion also how wold I go about make a transform.posistion (random between edge left and edge right)

avatar image whaleinthesea · Aug 11, 2015 at 08:15 PM 0
Share

new Vector3(-0.15f, .5f, 10) is a viewport a viewport starts at(0,0)left under. So (-0.15f, .5f, 10) is not exactly left under.

And for your second question here is the code: float f_random = Random.Range(v3Left,v3Right+1);//Random.Range give a float between first argument(v3Left)(included) and second argument(v3Right)(excluded, but we want that it can also spawn at the very right so we add +1 to it) transform.position = new vector3(f_random,y,z);//f_random is a random x position between the left and right position of the screen. Just replace the y and z with your own values'

So the in one line it will be: transform.position = new vector3(Random.Range(v3Left,v3Right+1,y,z)); I hope this helped you.

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to determine Android physical screen size? 5 Answers

How to adjust the screen for many types of screens in Android? 1 Answer

How can i run a Unity Build on my PC, but use my phone as second screen, (Cheapskate Oculus-Like) 2 Answers

Best way to sync between different mobile devices? 1 Answer

How to do .9 scale for scaling screen? 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