Thanks for taking the time, for your advise and expertise.
Yes, at first I also did think of using a physics engine but it demands quite a lot of resources (especially ammo.js) and I don’t really have another need for this physics in my scene. I want to spare as many resources as I can for everything that will be displayed and interacted with in my scene.
So, this morning I chose to first give a try at my alternate solution quickly explained above. I know, it’s not very ‘corporate’ and one could say " why ask for advise if you don’t want to follow them?" Again, my apologies for that. I certainly do value your expertise and in most cases would go with the solution provided by the experts. But here I found that my very simple alternate solution, not relying on physics and not relying on observables at all times would probably nicely do the job. And, well, I think I ended-up with a good solution for this use case.
I also had the same questionning as you stated about the potential jittering when pushing the camera (either with an imposter or a value through a beforeRender). Turns out that when I’m forcing the camera on the y.position only to match the position of my animated elevator floor, it seems to run really smoothly. I’m not sure it would be the same with a physics imposter (if I have some time, I will also check with your solution - would be nice to be able to compare, wouldn’it?).
I can also still move around and look around freely while the elevator is moving. Only thing is I cannot ‘jump’. But then, jumping inside an elevator is not recommended for safety anyways;)
So, here is what I sketched and plan to implement for all of my 5 elevators in the scene. It is also easy and simple because all elevators are already instances of ‘elevator’ and they are all sharing the same functions, logic and animations.
Scenario: When the player clicks on an elevator control panel to select a floor, the logic first checks if the doors are open or closed and, if open, closes the door before moving the elevator. This is where I run an onCollide check of the camera with the elevator floor to make sure the player is still inside the elevator when the doors are closing (because it would be possible to press a floor and next quickly run outside the elevator). On the next step, when the elevator starts to move, I already have a variable to record this state and I’m using this to run a beforeRender function pushing the camera on the y.axis alongside the position of the elevator animation. Here again, it works for all elevators at once, because they are instances and share all functions and animations. Finally, I also have an ‘after’ function triggered when the elevator reaches the destination. This is where I simply unregister the camera animation.
You can make an early check of this logic set on just one elevator and one floor. I now just need to move my function and calls to have it trigger and work for all floors and all elevators.
To check this behavior (at this early stage, sry for the buggy colliders, not done yet):
- Load the scene
Press ‘2’ key to switch to the first person camera
- Click the 'call elevator’ button on the console for the elevator that’s in front of you
- Enter this elevator
Select ‘hall’ level (this is where the function is currently implemented for testing)
- You can open the console and alongside of a ton of logs my small brain needed to understand my elevator state and behavior, you can notice when the function for moving the camera up is registered and when the function for checking the onCollide is register
- When at destination, notice that both have been unregistered/removed.
Hope you don’t mind me having found this alternate solution (for once I do have an alternate that seems to work… sort of
Well, thanks again and if I get the chance to try the version with physics, I’ll let you know.
Meanwhile, have a great day
Edit: Also thanks to @RaananW for debugging my camera.onCollide removal of event.
Edit2: I clicked @Cedric as a solution (although I will likely use my alternate solution) simply because I do believe it is a solution.