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 Tjhazmat · Dec 23, 2013 at 06:34 AM · camera2dinfiniteparallax

Infinite Parallax Background

Ok, so i have an idea for an infinite parallax background but have no idea how to code it, i'm pretty new to C#, so...

the player has the camera attached to them so as they move, they appear stationary but they are moving through the world. at the start of the level i want it to spawn an object at a random location inside the cameras view port.

i'm actually stuck right here...

 public float distance;
     public float lB;
     public float rB;
     public float tB;
     public float bB;
     public float randomX;
     public float randomY;
     
     // Update is called once per frame
     void Update () 
     {
         distance = (transform.position - Camera.main.transform.position).z;
 
         randomX = Random.Range (-10, 10);
         randomY = Random.Range (-6, 6);
 
         lB = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x;
         rB = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x;
 
         bB = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y;
         tB = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y;
 
     
         if (bB == 0) 
         {
             transform.position = new Vector3 (randomX, tB, -20);        
         } 
         else if (tB == 0) 
         {
             transform.position = new Vector3 (randomX,bB,-20);
         }
     }

from what i understand, the if statement is the problem, its not executing the transform.position.

I haven't even gotten to making a few hundred of these stars appear and all execute this code.

Comment
Add comment
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

2 Replies

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

Answer by robertbu · Dec 23, 2013 at 06:46 AM

I don't understand how this code is giving you a random position within the camera view. Here are a couple of points:

  • Never directly compare two floating point values...especially if one is the result of any kind of calculation. You can use Mathf.Approximately() to compare them.

  • Not sure it makes in difference, but the integer version of Random.Range() is exclusive of the upper value. So for example RandomX will get values from -10 to 9, and only integer values. I think you want this instead: randomX = Random.Range(-10.0f, 10f);

Now if you are trying to produce random positions in the view of the camera at 'distance' from the camera, you can use these two lines:

 Vector3 pos = new Vector3(Random.Range(0.0, 1.0), Random.Range(0.0, 1.0), distance);
 transform.position = Camera.main.ViewporToWorldPoint(pos);
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 Tjhazmat · Dec 23, 2013 at 07:15 AM 0
Share

Thank you for the advice on the randomX, i didnt know that.

$$anonymous$$y question was unclear, and its hard to explain.

The code I posted is an attempt at an infinite scrolling background, at least in the Y direction.

For now, i have a single sprite in the scene, as the player moves, the view of the camera changes, tB and bB calculate the Y value of the top and bottom boundaries respectfully of the cameras viewport.

The if statement checks to see if the sprite is still inside the boundaries. if the boundary passes over the center of the sprite, when tB = 0 if its the top boundary, or when bB = 0 when its the bottom boundary, the sprites position should update with transform.position to a random X value clamped to the width of my viewport, and a move to y value of the opposite boundary.

basically as the object passes out of view from the bottom it teleports to the top of the screen so it looks like its scrolling.

avatar image robertbu · Dec 23, 2013 at 07:29 AM 0
Share

You need to be comparing the transform.position to the viewport positions if this is your goal, and you need to use a more inclusive comparison. I can also see the potential for something to cycle between top and bottom. To start, pick one direction. Say you are scrolling up:

 if (transform.position <= bB) {
     transform.position = new Vector3(RandomX, tB, ??);
 }

I put the ?? because -20 is a bit weird. Typically for this kind of thing the camera is at -10 or -20 on the 'z' axis looking toward positive 'z'. So ?? probably should be 20. If this is not how you've setup your scene, then your distance calculation on line 12 is suspect.

Also this code will move the object as soon as the pivot point is off the screen. Likely you want the whole object off the screen. So something like:

 if (transform.position <= (bB - 0.25)) {
avatar image
0

Answer by Tjhazmat · Dec 23, 2013 at 10:28 AM

Wow, thank you so much, I've been struggling with this all day, haha, i cant believe i overlooked something so simple, it works like a charm, although the sprites clump up when i travel in both the X and Y directions at the same time but i can live with that for now.

Comment
Add comment · 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

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

19 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

Related Questions

Pixel perfect multiple cameras 0 Answers

parallax scrolling using orthographic camera 1 Answer

What's the most efficient method of implementing an infinite parallax background? 0 Answers

How to Manually Move the Camera Horizontally or Vertically? (2D, Parallax), 0 Answers

Problem rendering in 2D whit perspective camera 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