Centring camera on two objects and zooming - a solution

Here’s a snip of the code to keep the camera centred between 2 objects, Object1 & Object2. It also zooms in or out so that the two objects are always both on screen.

It will zoom in no closer than the original scene window size (the first part of _zoom = (min(1, ....), but will zoom out indefinitely.


Nice, I did something similar, but with an invisible cam_pointer object.

Is this the entire code? I checked I have everything correct but still does not seem to work.

This is everything to centre the camera and zoom, and it works. It won’t move your character or anything else. It’s purely the camera control.

For your example in your other thread, you’d need to replace Object1 & Object2 with Dile & Lori.

Yeah I did that. My character just kept going off screen but I could not see it.

Deli =/= Dile

That might be the issue. Ok, got It working. How would I make it zoom out before the character reaches the edge of the screen?

This is great! I have added this idea to the list of Extension ideas.

  1. Do you think it could be modified to work for all instances of objects? Meaning, keep all instances of an object on the screen? (this should work with object groups as well)

  2. Can you use LERP on the camera changes in case the objects move quickly to prevent jarring movement?

One way is to increase the value of extraWidth and extraHeight.

1 Like

I’ll try it out, thanks
Edit: So at first I tried changing the actual variable but then I realized that was not gonna work since at the beginning of the scene the value is changed to something else. cx Then this worked just perfectly : ) Thanks to both you for helping out.

If you create an extension for this please consider the following requirement:
There should be a MaximumZoom that is configurable, and when set prevents the tracked objects from moving in a direction that would cause the MaximumZoom to be exceeded.

This is a very common consideration for games that use this type of zooming.

Uh I just realized you opened a trello… I’ll post this there

1 Like

Hmmm… wouldnt the math for this break if both x or both y positions are negative?

E.g. width = 500 - 700 becomes 200 with absolute, but width = -500 - -700 becomed 1200 with absolute. Which is wrong.

However it is 4am so I could be thinking about this wrong.

Yeah, 4am doesn’t appear to be your optimal thinking time. -500 - -700 = 200 (minus a negative is the same as adding a positive).

1 Like

Yep. That would make sense. Less barely awake posts for me! :laughing:


How does your code work when there are multiple instances of an object? I expect it will set the variables based on the last instance created.

l was thinking about changing this to use a single object parameter (which can also be an object group) and do a ForEach() to find the min/max X/Y for all instances. Do you think that is a good design?

Yes, that’d be my approach too.

Something like :

It’s still not perfect, because in the loop it should ideally take into account the object’s size and position when calculating the extremes. But this’ll have to do for now.

1 Like

@MrMen this is great code! I should be able to make an extension quickly, unless you want to give it a try?

What text do you want me to use for the Author field? (can include short links or email)

@tristanrhodes, I need to admit I’ve never made an extension, so go ahead and make an it if you like.

I’ve had a think about the improvement I mentioned in my last post, and think I see a simple way to implement it.

Give me a PM about the author field text and any other details you need, and I’ll also see about getting you the code in JSON or some form that you can copy and paste easily.