-1

I need move unity3d camera to position in which camera will cover all selected object. I mean position in which all selected points will be inside camera frustrum. How to calculate camera position?

Like in this example: enter image description here

code11
  • 1,986
  • 5
  • 29
  • 37
Dimcore
  • 21
  • 2
  • What did you try so far? Do you have some code to show? – Ludovic Feltz Apr 13 '17 at 11:56
  • It's not the problem of code. It's just algorithmic problem. I don't know how to calculate camera position if I have points which have to be in frustrum. Maybe I need formula of pyramid which contain selected points – Dimcore Apr 13 '17 at 11:57
  • I have tried to use screen (camera flat coordinates) positions of this points. – Dimcore Apr 13 '17 at 11:59
  • So where is yours? What is your problem. there is a lot of position you camera can be to see all your selected points. For example just set your camera position very far from all your point should do the trick – Ludovic Feltz Apr 13 '17 at 12:00
  • http://joxi.ru/LmGD5gLSeDKnE2 – Dimcore Apr 13 '17 at 12:08
  • I mean position in which my objects will be on the borders of camera frustrum – Dimcore Apr 13 '17 at 12:09
  • http://joxi.ru/krDDjg6SExWB1r – Dimcore Apr 13 '17 at 12:10
  • In runtime camera shold be scaled and moved into the closest point where it covers all selected points. For example, I have camera with Field of view - 60 degrees and I need to move it to the points where it will cover all needed points. – Dimcore Apr 13 '17 at 12:12
  • 1
    Possible duplicate of [How to Fit Camera to Object](http://stackoverflow.com/questions/14614252/how-to-fit-camera-to-object) – Pavel Pája Halbich Apr 13 '17 at 12:15

1 Answers1

0

Put all the objects in an array, than compare the distance between all of them and take the biggest one.

Then you just have to make a calculation and apply that to your camera fov/range so that it covers all the objects.

I already made something like this before this way in Unity 2D, so it should be possible in Unity 3D. The problem is that I couldn't find the source of the Unity project but I did found a game exe.

Link to zip (I will update this if I can find the project)

NLxDoDge
  • 189
  • 2
  • 13
  • "then you just have to make a calculation and apply that to your camera fov/range so that it covers all the objects." - Hah. That is think I asked about – Dimcore Apr 13 '17 at 12:54
  • With the calculation I mean the correction. ` 2D: Camera.main.orthographicSize = MaxDistanceInBlocks; 3D: Camera.main.fieldOfView = MaxDistanceInBlocks; ` – NLxDoDge Apr 13 '17 at 12:59