|
|
|
|
Reply From: |
Wadah |
Hi
I believe its hard to find something similar online , I am not a pro and not experienced with Vector2D , there might be some built in functions to handle this and am just re inventing the wheel .
Its also my first time to answer but I liked the question so I want to get it starting for you I will give a mathematic/logical answer and WILL NOT WRITE ACTUAL GODOT SCRIPT its just pseducode since I can’t access my Godot editor right now .
First i will write down my mindset
!!!
Here I assume Godot vectors work same as math vectirs at algebra lessons
Example:
Vector a(5,3)
Vector b(-3,-3)
Vector c(-1,1)
The vector between a and b : (1,0)
The vector between a and c : (2,2)
…
I assume vector magnitude = vector.length() in godot
Eg vector (500,1720) have magnitude of 1791.2
Vector (100,402) have magnitude of 414.25
If Godot Vectors act different please inform me to add the math calculation formula
!!!
In order to make the camera focus both of the players it must be placed in between
And zoom in-out according to the distance between them
So for camera position I will : in pseducode
.
# start
Get 1st player position
Get 2nd player position
Calculate camera position= 1st player position- 2nd player position
Set the the camera position to the variable above *0.5
# end
.
Godot camera zoom works with x and y values with 1 as default value
Increase both camera zooms out
Decrease both camera zooms in
now that we have the camera in the middle , we calculate distance from camera to any of the players as reference .
for the the zoom , you can either use range (steps ) zoom or calculated (real-time) zoom
Steps zoom:
using number of if elif statement
Eg .
.
# start
Zooming=1 #stores selected zoom
Cam_position = camera position
PlayerA_pisition = Player a position
Zoom_vector = cam_position - playerA_position
If Zoom_vector.length()<= 200 :
Zooming=1
ElIf Zoom_Vector. Length()<=250:
Zooming =1.1
..... Till the last elif
Camera.zoom=Vector2(zooming , zooming )
#end
.
You will have to experiment with outcome of different distances between players to get this right , use print function to print the distance .
…
Calculated zoom :
take the distance (cam-player) itself and offsets it to match the property of the camera , for example to give range of ( 1 - 1.5 ) which is (start zoom , maximum zoom out )
To get this right , use print function to print the distance between players and distance between player and cam
For example if you find its comfortable to start zooming out from (500,1720) distance between players, (250,860) distance between player and camera
With Magnitudes : 1791.2 and 895.6 respectively . ( we use magnitudes because they express distance , using victors as ranges here will introduce tons of issues)
This gives you maximum zoom out at around 2600 and starts at 1791.2
Let’s get to the pseducode
Your offsetting value would be 895.6 ( as am using the cam-player as reference)
.
# Start
Offset = 895.6 # you find by experimenting as explained above
Zoom_vector = as calculated in the last pseducode
Zooming = zoom_vector / offset
If (zooming <= Zoom_start || zooming >=Zoom_max):
Alert_not_zooming(zooming) #function to alert other scripts
Return # Don't zoom outside the limits , it also saves processing power
camera . zoom= Vector2(zooming,zooming)
# End
.
Notice : this might introduce some glitches or issues depending on your game scale .
Use the Alert_not_zooming(zooming) function to use it for example to tell players to not go further from each other or to limit their movement accordingly
As final word . thank you for being patient enough to read and try to implement my answer in code , I will have email notifications allowed so I can answer any question or issues you might have after trying this