Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Danisuper · Jan 20, 2016 at 09:05 PM · 2d2d-platformerparallax

GeometryUtility.TestPlanesAABB working strangely with parallax background 2D

Hello all I started working on a parallax background for my 2d game. Everything worked fine,except when I had to make an object pool that recycles the tiles of background that the player left behind him and move it at the opposite side of the line of background tiles(a lot of looping sprites all spawned in an array wich is backs in the code). To check if a tile is out of the camera I use GeometryUtility.TestPlanesAABB but it works stragely in fact the tiles don't change the position to go to the other side when they are out of the camera's sight but when they are in the middle,at the level of the player. Here's the piece of code with the problem:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class ParallaxScroll : MonoBehaviour {
 
     public GameObject[] backs; //all the spawned backgrounds are stored here
     GameObject player;
     float focal_speed;//velocità di movimento del player da cui dipende lo scrolling
     public float[] back_speeds; //the speeds of the different layers of background based on the depth
     bambina_inputs bambina_vars=new bambina_inputs();
     BackgroundManager back_manager;
     Plane[] planes;
  
     void Start () {
         backs = GameObject.FindGameObjectsWithTag("backgrounds"); 
         player = GameObject.FindGameObjectWithTag("Player");
         bambina_vars = player.GetComponent<bambina_inputs>();
         back_manager = gameObject.GetComponent<BackgroundManager>();
         planes = GeometryUtility.CalculateFrustumPlanes(Camera.main);
     }
  foreach (GameObject back in backs)
         {
             Renderer back_renderer = back.GetComponent<Renderer>();
             //if it's out of the screen
             //DX
             if(!CheckIfInScreen(back_renderer) && back.transform.position.x<player.transform.position.x && Input.GetKey(KeyCode.RightArrow))
             {
                 MoveToTheOtherSide(back, back_manager.x_offset, back_manager.n_backs);
             
             }
             //SX
             if (!CheckIfInScreen(back_renderer) && back.transform.position.x > player.transform.position.x && Input.GetKey(KeyCode.LeftArrow))
             {
                 MoveToTheOtherSide(back, back_manager.x_offset, back_manager.n_backs);
 
             }
         }
      }
 
     //moves the object to the opposite side of the background
     void MoveToTheOtherSide(GameObject g_to_move,float x_offset,int n_tiles_to_move)
     {
         //made the casting so the Input.GetAxis return value is only 1 or -1
         g_to_move.transform.position = new Vector3(g_to_move.transform.position.x + (x_offset * n_tiles_to_move) * (int)Input.GetAxis("Horizontal"),
             g_to_move.transform.position.y,
             g_to_move.transform.position.z);
     }
     //checks if an object is in the screen
     public bool CheckIfInScreen(Renderer renderer)
     {
          bool in_screen = GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
          return in_screen;
     }
 }

What's wrong? And also don't hesitate to suggest me a better way to make a parallax scrolling (I now that one sucks,I already gave a look to the one in the example project but i wasn't working good with my background).Thank you very much for your time and your help :D

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
1
Best Answer

Answer by Bunny83 · Jan 22, 2016 at 05:43 AM

Is your camera fix? Or does it move? It it moves (rotate / translate) you have to update your camera planes. Also your code looks like you've cut something out after the start method, most likely the Update method ?!

The next thing is your casting to "int" in line 44. If the input value is slightly less than 1 it will be clamped to 0, not to 1. Casting to int doesn't round the value but truncates the value. If you just want the sign of the value, use Mathf.Sign which will return either "-1" or "1". "-1" will only be returned when the value is less than 0. If the value is 0 or greater it returns 1.

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 Danisuper · Jan 25, 2016 at 08:54 PM 0
Share

Thank you very much,it's working fine with GeometryUtility.TestPlanesAABB now.

avatar image
0

Answer by Danisuper · Jan 22, 2016 at 12:36 PM

Today I gave a look again at the code and I tried to change the !CheckIfInScreen(back_renderer)with the normal GeometryUtility call. Both didn't work fine and the tiles of the background changed the position to go to the other side when they were in the middle of the camera. Then I tried to change the GeometryUtility.TestPlanesAABB with !back_renderer.isVisible and now everything works fine. But why it didn't worked before with GeometryUtility? I used it also in some other projects and GeometryUtility worked fine...That's pretty strange

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My parallaxing is moving by it self? 1 Answer

2D Coins and Points 1 Answer

Best 2d User Interface for Mobile and Other platforms 1 Answer

Changing SpriteRenderer order in script 1 Answer

How to reduce moveability for player when releasing button mid-air 2 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