siamenock
siamenock

Reputation: 109

Unity, how to get Camera angle Width

I'm making game that must set target number on all enemy on screen.
I FAIL TO FIND function like IsInsideCameraView(gameObject)

Now I'm trying to scan area that camera looks. For this, I need Camera angle width, angle hight. As described in this emage (My reputation is not enough to show image directly)

My question is similer to this question, How to get angles value of perspective camera in Three.js? but for Unity.

If you know
HOW TO CALCULATE CAMERA WIDTH&HEIGHT (following screen size when unity starts)or
IS THERE GOOD FUNTIONS LIKE IsInsideCameraView(Camera, gameObject)
any answers will be welcomed.

Upvotes: 0

Views: 866

Answers (2)

user585968
user585968

Reputation:

I FAIL TO FIND function like IsInsideCameraView(gameObject)

You can use:

object.Renderer.isVisible

...to check if the object is visible in any camera.

To test for a specific camera, use:

Vector3 screenPos = camera.WorldToScreenPoint(target.position);

Then check if X and Y coordinates are between (0,0) and Screen.width, Screen.height.

HOW TO CALCULATE CAMERA WIDTH&HEIGHT

That's what Camera.pixelWidth and Camera.pixelHeight are for or you can use Screen.width, Screen.height for cameras that represent the entire screen.

Anyway, that's arguably a XY problem, refer to the beginning of my answer for direct solution.

Tell me more

Upvotes: 1

student.a
student.a

Reputation: 33

[Untested. Javascript] You can use renderer.isVisible to determine if the object is within the camera frustum.

Upvotes: 2

Related Questions