Maintain Arc Rotate camera rotation while changing camera Z position with GUI slider


How might I maintain rotation on an ArcRotateCamera when changing camera Z position using a GUI panel slider?

Here’s the very basic code:
slider.onValueChangedObservable.add(function(value) {
camera.setPosition(new BABYLON.Vector3(0,0,-10+value));



Hiya Galen, welcome to the BJS forum!

Umm… I have an idea. When you move camera.position.z, also move… the exact same amount. Might work. :slight_smile:

You might want/need to set an invisible targetMesh as, and then just move targetMesh the same AMOUNT as you move camera.position.

Also, you might wish to set camera.lockedTarget =… or maybe camera.lockedTarget = targetMesh.position. THEN… MAYBE… you only need the slider to move the (invisible) target itself, and the camera will move WITH IT.

I’m not an expert in using camera.lockedTarget… sorry. You’ll need to read docs, search playgrounds, and do tests… to learn its full story. :slight_smile:

Goof around with that stuff… and report back, as wanted. Other/Better replies might come soon.

Hey @Wingnut,

Long time! I hope you are doing well. I don’t have any issue with maintaining my camera target; however, I played around with it for the past 30 minutes anyway. I’ve tried to do this with a Universal camera and other types, but with no luck. It can’t be that hard, right?



1 Like

Like this?

Hi guys.

First, @Rah, Galen doesn’t want the camera to turn… when the z-zooming happens.

Yours is turning. :slight_smile: You need to move the same amount… and you shouldn’t force camera x and y to zero, as that over-rides CURRENT camera x/y. I adjusted that… in #22. :slight_smile:

Galen… good to see you again, too. I’ve been well, thx. Yourself?

I tried one…

Added line 243… and then installed some good greasy code-lines in 254-259.

I think it’s working, but it’s Monday, which means I’m not fully operational. :smiley:

1 Like

Yeah just as i came back to reread the question, as you posted. lol

Anyway uh not sure why the others turn, but this one doesnt

Wait nvm… gimme a minute

Seems to be an optical illusion?

1 Like

heh. No sneaking-in that camera.position = new BABYLON.Vector3(0,0,0); in line 10, Rahmeister. :slight_smile:

Camera must use CURRENT position… set in line 8. Then add to that. Still turning. :slight_smile:

You must move, too. And yes, when camera.position is perfectly x/y-aligned with cam target x/y… then moving z position won’t turn the camera. You’ll need to set the camera at a weird angle, and do your tests from there, I suppose. :slight_smile:


Haha yeah i wanted to make sure haha gotta have a good starting position :slight_smile:

However what i meant was check the console, the camera is not actually rotating, nor are the cubes(spheres), there must be some kind of internal math that is changing like zoom on a camera…

1 Like

It looks like an illusion of perspective to me, as it only seems to “rotate” when viewing a corner. When zooming from one of the 90 degree angles on a flat face it seems to just zoom in and out how you’d expect.

But your guesses are as good as mine :joy:


@Rah and @Wingnut

I really appreciate the assistance with this. As I’m not at all familiar with the babylon.js GUI panels, it took me a few minutes to figure out what the heck @Rah was doing. However, I picked the PG scene apart and scaled it back allot to get at the heart of the solution.

The issue, however, is that when I rotate the camera and then move the slider, the rotation remains the same. I haven’t yet figured out the math to hold camera rotation which was my initial problem, so your HUGE brains might come up with a solution before I do. Also, I gotta make some eggs as I’ve been up coding all night.

Don’t know how much more brain power I have right now… but will try to keep going once I get some little chickens in my gullet.:yum:



Sorry if I don’t quite understand your question, but can’t you just change the camera.radius? perhaps in combination with changing the z-value?

1 Like


DK and other devs might remember that I don’t do my best work when I don’t sleep (or eat). Yes, attaching the value of the slider to the camera radius works fine.:grin:

It’s been so long since I’ve worked in BJS, that I overlooked this.

Thank you!


1 Like

@Galen Good to hear haha. Yeah, we’ve all been there before… sleep is important haha. Food too :smile: