Hello! I was wondering… is there a way to make my game only update what is within my camera bounds? Updating a little bit outside the view works as well! I was searching google and I couldn’t find anything on the subject.
I got lots of movement going on in a single scene so doing this is going to help players with… ehm… less powerful computers. I am using Orthographic Camera in a 2D and making a platformer.
You can get the SpriteRenderer and check if it is visible. If it isn’t visible you can just return which will ignore the rest of the computation.
Something like this might do
SpriteRenderer renderer;
void Start () {
renderer = GetComponent<SpriteRenderer>();
}
void Update() {
if (!renderer.isVisible) {
return;
}
//the objects behaviour to follow here
}
Yes there is I do it with my game, although I do it differently to andrewmcglynn86,
I have these functions in a script attached to the camera.
public Rect CameraScaledViewPort(float xscale, float yscale)
{
Rect mRect = CameraViewPort();
float mTilesInHalfWidth = ((mCamera.pixelRect.width * xscale) / Tilesize);
float mTilesInHalfHeight = ((mCamera.pixelRect.height * yscale) / Tilesize);
float mLeft = (mRect.xMin - mTilesInHalfWidth);
float mTop = (mRect.yMin + mTilesInHalfHeight);
float mRight = (mRect.xMax + mTilesInHalfWidth);
float mBottom = (mRect.yMax - mTilesInHalfHeight);
return new Rect(mLeft, mTop, mRight - mLeft, mBottom - mTop);
}
public Rect CameraViewPort()
{
return new Rect(mLeftTile, mTopTile, mRightTile - mLeftTile, mBottomTile - mTopTile);
}
public bool ObjectCoordinatesInCameraView(int x, int y)
{
if (x >= mLeftTile && x <= mRightTile)
if (y >= System.Math.Min(mTopTile, mBottomTile) && y <= System.Math.Max(mTopTile, mBottomTile))
return true;
return false;
}
public bool ObjectCoordinatesInScaledCameraView(float xscale, float yscale, int x, int y)
{
Rect mRect = CameraScaledViewPort(xscale, yscale);
if (x >= mRect.xMin && x <= mRect.xMax)
if (y >= System.Math.Min(mRect.yMin, mRect.yMax) && y <= System.Math.Max(mRect.yMin, mRect.yMax))
return true;
return false;
}
this code is used to follow the player around, because I scale the original camera view, I allow only gameobjects to move/render in that scaled view, because you need some overlap so the player does not see the gameobjects been re-rendered again.
and to check it lets say in the monsters update I use the following
mInView = mCameraComponent.ObjectCoordinatesInScaledCameraView(0.1F, 0.125F, (int)this.gameObject.transform.position.x, (int)this.gameObject.transform.position.y);
if (mInView == false)
{
if (mRenderer.enabled == true)
mRenderer.enabled = false;
if (mAnimator.enabled == true)
mAnimator.enabled = false;
return;
}
else
{
if (mRenderer.enabled == false)
mRenderer.enabled = true;
if (mAnimator.enabled == false)
mAnimator.enabled = true;
}
I’m not sure if this is what I’m looking for. What I mean is to have the game “only update objects that are within camera bounds”. Meaning if the object is outside the camera bounds it shouldn’t even go into those objects script and update unless it is within camera bounds. I guess there is nothing on it, huh?`I guess I can use your method if I can’t find anything else. Thanks for helping ![]()
Here’s two possible solutions:
Give your objects a trigger radius that enables the script when the player enters, and disables the script when the player leaves. Disabled scripts still receive collision events.
Use the built-in MonoBehaviour callback functions OnBecameInvisible and OnBecameVisible to manage your enabling and disabling.
Note that your editor scene camera will also trigger these events.