- Home /
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
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.
Thank you very much,it's working fine with GeometryUtility.TestPlanesAABB now.
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