I’m programming a camera for a first-person game. Right now, I’m working on a feature that will allow the player to look behind them while holding the ‘Q’ key. The camera will slowly pan 180 degrees horizontally while Q is held, and when let go will pan -180 degrees to the original orientation. Currently I’m using TweenService to achieve this, though if I end up using something else, I’ll still have one fundamental issue.
The issue is this: lets say you hold Q to pan 180 degrees, but you let go of Q halfway through. Now the script will try to subtract 180 degrees to reach the original orientation, but obviously it won’t. I fixed this by using os.clock() to time when each Tween started, then subtract that from the time recorded when the Tween was interrupted. I could then divide the elapsed time by the tweenInfo.Time and find its percent completion, and use that to find how many degrees I needed to pan the camera to return to its original orientation.
Not only does this feel overly complicated, but it also doesn’t fully solve the issue. Here’s another example: let’s say you did the same steps as before, but then you pressed Q again, interrupting the Tween once again. Now it’ll try to use the original interruption as the starting point to return to. Letting go of Q will then use that interruption as the starting point, and so on.
This is where it starts getting really confusing for me. I can’t wrap my head around any fix to this issue. An easy fix would just be to force the Tweens fully play out instead of interrupting every time Q is pressed or released, but this is not what I want. Let me summarize exactly what I want here:
-
I want this feature to be spammable. I should be able to press and release Q, interrupting the Tweens as many times as I want and have the camera return to its original orientation.
-
It needs to work while the camera is being moved around in other ways. Whether the player is walking, panning their camera around, etc., the ‘look behind you’ feature should smoothly rotate along with the player.
I’m not asking for a completely brand new script. I just need some ideas so solve my current issue. If you think there’s a better way to approach this entirely then feel free to say so. Let me know if you need further clarification