|
|
|
|
Reply From: |
eons |
The thing is that the camera there never follows the players but the center of the players.
In godot you can simplify a bit things and make a node as a camera focus (parent of the camera), that node will try to be always in the center of the action, the distance to the nodes the center follows will define the zoom of the child camera (you can make it smooth to add some effects when the camera follows the center).
This style can be used with many players, enemies or something important on the scene, just put all on the same group and search for the max and min xy pairs to get the center position.
An interesting way could be using a moving path, the path with 2 points moving with each player, a pathfollow on unit offset fixed on 0.5 with a child camera, the camera can get the distance with the offset to make the zoom.
This I think has less calculations and fast to prototype, will work easy with 2 objects, but may have problems with zoom.
I don’t have any in mind but…
You can do this with a camera as children of a center node or directly with the camera (I prefer to leave the cameras alone when possible).
The camera/focus position will be updating constantly at (node1.get_pos()+node2.get_pos())/2
, or global pos if the scene is more complex.
Camera should follow smooth if possible for better look, drag margin can be a problem when zooming.
Something quick and dirty and using the camera directly instead of a parent as focus:
edit: realized that got a problem with the code, you get NodePath from export, could be better to assign groups to the player and just call that group to get an array of players
extends Camera2D
export(NodePath) var player_1
export(NodePath) var player_2
var p1
var p2
func _ready():
p1 = get_node(player_1)
p2 = get_node(player_2)
if p1!=null&&p2!=null:
set_process(true)
func _process(delta):
var newpos = (p1.get_global_pos()+p2.get_global_pos())*0.5
set_global_pos(newpos)
#then comes the zoom
For zooming, get camera/focus difference with positions of one of the nodes and use that as a zoom factor to set a target zoom (adjust until it feels good for your game).
Or distance_to could be useful for this too.
Depending on the type of zoom you want, you may need to make the zoom factor related on the viewport size to keep characters to the same distance from the border of the screen.
Trying to get distance from players like this
func _process(delta):
var newpos = (p1.get_global_pos()+p2.get_global_pos())*0.5
set_global_pos(newpos)
var distance = Vector2(p1).distance_to(p2)
but it says
Invalid call. Nonexistent 'Vector2' constructor.
everancii | 2016-12-22 17:53
distance_to gives a float, it should be:
var distance = p1.distance_to(p2)
Then use that to create the zoom factor.
If you just multiply the distance to a fixed small number(like 0.0005), higher zooms will give bigger separation between players and border of screen (lineal).
A lineal zoom modification may look good on some games, if not a bit of math work will be needed to get the perfect equation.
Finallly, when you got it (be distance*number or something more complex), just do:
set_zoom(Vector2(1,1)*zoom_factor)
Trying like this
func _process(delta):
var newpos = (p1.get_global_pos()+p2.get_global_pos())*0.5
set_global_pos(newpos)
var distance = p1.distance_to(p2)
var d = distance * 1
get_node("Camera2D").set_zoom(d)
But it says Parser Error: Unexpected token: Identifier:get_node
everancii | 2016-12-22 20:51
The indentation is the same as that code block? because then you are writing the get_node outside the function
Yeah this is was indentation ^^
Now i got this error
Invalid call. Nonexistent function 'distance_to' in base 'RigidBody2D
everancii | 2016-12-23 00:19
Solve my problem and make it work with this script
extends Camera2D
func _ready():
# Initialization here
set_process(true)
func _process(delta):
var p1_pos = get_node("/root/world/Player").get_pos()
var p2_pos = get_node("/root/world/Computer").get_pos()
var newpos = (p1_pos+p2_pos) * 0.5
set_global_pos(newpos)
var distance = p1_pos.distance_to(p2_pos) * 2
var zoom_factor = distance * 0.005
set_zoom(Vector2(1,1) * zoom_factor / 2)
But now zoom is too high, from player to margin a very long distance when start zooming out
everancii | 2016-12-23 11:26
Keep playing with the zoom factor, use smaller numbers until it feels good for your game.
Thank you for your help
everancii | 2016-12-23 15:27