How to Calculate if a gameobject is inside of the rectangle

Hi I’m trying to find if a gameobject inside of a rectangle . I was using diagonal to find but it is not rectangle but circle

More context needed. 2d game? 3d game?

In either case you can probably get away with using either the 2d or 3d version of OverlapBox.

https://docs.unity3d.com/ScriptReference/Renderer-bounds.html

2

it is a 2D game

I looked at is it takes boundries of the rectangle if that so ı should use closest point of it right ?

Tell us what is your specific case.

I was trying to hide the spriterenderer of the object that out of bounds of the camera

Is camera orthographic or perspective ?
Is your spriterenderer facing camera directly or might be at some angle ?
Can sprite be rotated ?

orthographic it is a 2d game I dont think angle do anything or rotation I will try boundry didnt try yet

The question is how much precision you need for this. (If you got rotated sprites, for Axis Aligned Bounds it is not an issue)
Yes try with bounds should be good enough for your need.

Why rotation is a problem

The Axis Aligned Bounding Box changes with rotation.

Unity automatically performs frustum culling on all Renderers. Is there a specific reason you need to do something more than that?

no what is frustum culling

Frustum culling automatically ignores renderers that are not in the camera’s view. Here’s an illustration of what that looks like in action. Note only things within the camera’s view are being rendered here:

You don’t have to do anything special to enable frustum culling in Unity, it happens automatically.

where is it is it on the preferances

It isn’t anywhere in preferences. As I mentioned above:

well it is now working ı guess it doesnt work on 2d

Camera.main.GetComponent().bounds.ClosestPoint(enemy.transform.position); I tried this but it says there is no renderer that attached to the camera

Why do you say it is not working? It is always working. It works in 2D and 3D. You won’t see it working in the editor because Unity also considers the scene camera as a camera and won’t cull things visible there. But it will work in your game no problem.

If you want to visualize frustum culling in scene view as per my example screenshot above, you need to have the Occlusion Culling window open with the settings to visualize as per the top section of this article:
https://jaredamlin.medium.com/understanding-frustum-and-occlusion-culling-in-unity3d-9b15cc2b078a

It’s a little confusing I know.

Anyway the point is if you are just trying to implement frustum culling yourself, you are wasting your time.

1 Like