- Home /
Keeping two or more objects in a camera's view.
I am making a fighting game where the player and the opponent move around in 3D space, as opposed to 2D space as with a normal fighting game. I need the camera to always have the player and the opponent in view. So far, I have created a sphere that updates its position to always be in the exact middle between the player and his opponent, and have my camera looking at that.
I can't figure out how to zoom out when a character moves out of the camera's view or how to zoom it back in when a character moves closer to the opponent. I also need it to rotate in an appropriate manner when one character is above/below another.
I guess the closest thing I can think of for an example would be the camera in Dissidia Final Fantasy.
Answer by BoredKoi · Aug 31, 2010 at 06:12 AM
Phew, nothing like trig to keep the mind going far too late. Anyway, I'll give this a shot for a solution at least in the horizontal plane. I think this could easily be expanded to handle additional vertical offsets. But onwards:
Given Characters A and B, Sphere S, and Camera C:
A -- S -- B
C
What we need to do is solve for the distance between S and C every update cycle, AND keep the our angles constant as far as the camera field of view is concerned (i.e., when you set your scene, you are saying that each character visually will be offset by X degrees from center). Some Mathf.Clamping will be necessary for the boundary situations but this -I think- will get you fairly close to the desired result:
- Start with the centered sphere looking at one of the players (B in this example)
- Parent the camera to the sphere, and have the camera looking at the sphere
- As the players move and the sphere is re-centered, "looks", and rotates, that will swing the camera in an arc and maintain the 90 degree A-B / S-C angle.
- Run the following calculation (in this example, from the perspective of player B)
- ( Vector3.Distance(S,B) * Mathf.Sin(Vector3.Angle(S,C) ) / Mathf.Sin(X) << note our constant "X" offset angle here
- Set that as the camera distance from the sphere
For reference, the formula above is the Law of Sines, just solved for our S->C distance.
This is totally untested, but I think the approach is right. Hope it helps. I'll be trying it out in the AM myself!
Thank you for the detailed answer! I will try to implement this when I get a chance to work on it next.
Your answer
Follow this Question
Related Questions
Camera Shake Effect When Camera Following Player 1 Answer
Click and Drag 3D Camera, like Anno 1 Answer
Jerky 3rd Person Camera Following Movement and Rotation 0 Answers
Moving between two cameras 1 Answer
Panning camera based on a fixed point. 0 Answers