How to let a stationary camera focus/follow a moving target?

Hey folks,

so I want my camera to be stationary aka not changing its position but keep focussing a moving target. So the camera should only rotate with the moving target always in center of the screen (until it disappears behind an object).
The camera should not be user controlled.

I tried using the target property but as far as I understood setting this property on a FollowCamera/FreeCamera will lead to the camera moving in a set distance to the set target instead of being stationary and only rotating.

Thanks for you help.

Hello! What about the ArcRotateCamera? Did you try it?

1 Like

Doesnt the ArcRotateCamers rotate/move around the target obeject? I just want the camera to stay in place and only change its y-rotation so the moving target object stays in the center of the screen. It is more like an inverted ArcRotateCamera with the camera in the middle tracking an object which is moving around it.

Can you prepare a Playground with the moving object? I bet we can figure out it no time which camera is suitable for you as well the underlying implementation should not cause any problems.

Within the playground I achieve the desired behavior by using a FreeCam and setting the lockedTarget property to the mesh to focus. So I guess this should also work within my scene. I just need to figure out why that’s not the case…

Seems like the TransformNode parented to the camera is causing the problem. Its rotation and translation seems to interfere with setting the lockedTarget property.

Any solution to this? I’m not that familiar to matrices and views.

I could have misunderstood it so my apologies if this is.
I’m just wondering what this is with this transform on the y-axis?
You want to follow/target the object so that it is always in the center of screen, is that correct?

2 Likes

Just came here to look at the PG created by @Brewcrew-MJ and it seems you’ve already solved the problem :slight_smile:

2 Likes

Not sure. I actually don’t understand the y-rotation thingy (in the text and PG). We’ll see…

2 Likes

Yes, you are correct.
I think I need to give some more context.

So in my project the user (camera) is walking down a path. At the same position as the camera there are two lights being placed, illuminating the path in front. Therefore camera and lights are being parented to a TransformNode (which is then animated along the path → “walking” animation).
Along the path “events” will happen. For example a bat flying by, things crossing the path, etc. … At those event-points the “walking” animation will pause and the camera should track the “event” (for example track the bat flying by and disapearing in the dark) before resuming to walk down the path after the event ended.

image

That’s why I added the TransformNode into the playground and changed the y-rotation, because in the project rotation and position of the player (Camera + Lights) are constantly changing. And that’s when I found out that setting the lockedTarget property of the camera while also having a rotation at the parented TransformNode will cause the camera to not track the object as planned.

The first thing that comes to my mind is removing the parented TransformNode, instead directly parent both lights to the camera object and animate the camera object instead of the (now removed) TransformNode?

Any thoughts? Or is there a work around where I can multiply some matrices and vertices to get the correct position to point at (but then again the events position is animated so the cameras target position would have to change as well)?

Interestingly not setting the lockedTarget property but the target property instead leads to the correct result for the “initial focus” but won’t track the object’s movement.

Maybe it’s because setting the lockedTarget property disables panning?

Define the current target of the camera as an object or a position.
Please note that locking a target will disable panning.

So I found a workaround. When the object-to-track’s animation is started I gonna add and onBeforeRenderObservable which is setting the camera’s target property over and over again as long as the objects animation isPlaying. After the animation ended i remove the observable.

1 Like

I’m happy you found the solution on your own :relieved: because - thanks for the context - but this caused me even more confusion and I would have had many more questions, like:
So, is it actually a first person camera view? the camera is the player and moves like in an FPS? He follows a path, does this mean movements are restricted? When the event occurs, the user inputs/view is frozen and the ‘follow event’ behavior/animation kicks in? Once the event is over, freelook and user movement inputs are restored (with or without restrictions)? … :sweat_smile:
Though, now that you found your solution (GJ :+1:), you don’t have to answer, of course :grin:… but may be, if it’s not too much effort for you, you could share your logic for other users having a similar use case? That would be great (not a request, just a suggestion). Meanwhile, have a great day and GL with your project :sunglasses:

1 Like

I’m sorry for the confusion :sweat_smile:
Sure, I’ll happily share my “logic” here to help others (although I do not know if my solution is the “best case” or even performant. But I’ll try to give some more information:

In my project a first person camera (representing the user) is animated along a path (I will call this the “walking” animation). This camera has - for the moment - no controls at all (not during the walking animation as well as not during the events that happen along the path), although some slight rotation (like looking left/right) might be added. Because the user has no influence on the walking animation, the “walking down a path” is more like an experience. At some (for the user random) points along the path the walking animation will pause and an “event” will happen. Those events are objects crossing the path from side to side or just take place right next to the camera. The moment an event is triggered the camera should pan/rotate towards the event and track it as long as the event lasts so the events origin is always in the center of the screen. After the event has disappeared the camera pans back down the path and the walking animation resumes to play.

Maybe it’s easier to understand if I use a made up example:
Imagine it’s night, you’re walking down a path through the woods. Because you are scared you do not want to /can not look left/right or stand still. Then suddenly you hear noise nearby. You automatically stop walking and look towards the noise. The source of the noise is a small hedgehog which is now crossing your way from one side of the path to the other. You watch it pass by, until it disappears and then continue your journey down the path through the woods. This keeps going until (maybe) you reach the end of the woods.

Yes I know, everyone would start running when the noise appear :sweat_smile:

Although I think using/setting the lockedTarget property might actually be the best solution it doesn’t work for me as the camera is always pointing in the opposite direction (like in my first playground). Using the target property lets the camera point in the correct direction (but won’t track the object as the property is only made for Vector3). I do not know if this is the case because setting the lockedTarget property breaks camera panning, but I found a workaround (as I said I have no clue whether this is a “good” solution regarding performance, etc.): the moment an event triggers (and therefore the walking animation pauses) I create a panning animation for the camera towards the events position. After the animation ended I register an Observable for onBeforeRenderObservable which is re-stetting the cameras target property over and over again (with the event’s current position) so it is tracking the event. The moment the event’s animation ends a new camera panning animation is created and played so the camera is pointing down the path again. After this animation the “walking” animation resumes.

Hope this gives some more clarification. Thanks for all your help and input.

1 Like