I wrote a reply for something similar that has a lot of resources you can use:
The full reply:
Realism — Make your games feel more immersive!
- My personal choice, though the camera sway comes from the animations instead of code, so it’s not as clean as doing it algorithmically. It’s drag and drop though, and adds lots of other immersive features for first person (I assume you’re doing a horror game from the reference).
The ones below are really simple to add, but this module lets you make immersive animations without view models and the client-server replication code you’d need for that. If you’re not looking for that, you can use the ones below (though I’d still recommend the foot step sounds from the one above, even if you disable the other features).
These might be good too:
Open FPC - A Modern First Person Camera System
- (This one has everything to do with what you’re interested in)
First Person Camera Movement Sway and Idle Sway
Free Camera sway script (First Person)
EasyFirstPerson: Drag-and-drop first person view models!
One way of making this is you use a wave function like sine or cosine for the swaying, and use a spring module (Quenty has a great one in nevermore engine that I use) to smooth out the camera animations (a spring basically adds physics in a way, so you set a goal and it “springs” to the goal you set). You can also use the spring to add a “weighty” feel when you rotate the camera that feels immersive.
I would use the things above though, as they’re easier and probably mostly implemented at a fairly skilled level (realism especially).
Hopefully that reply is useful and maybe some of those work well for you!
Generally a mix of trigonometry equations like you mentioned (usually sin/cos, for algorithmic back and forth movement) and springs (for a weighty, physics based feel) are used.
For example, to get the camera tilt, you could use a spring to control the roll of the camera, and apply a force to the spring based on the camera’s horizontal (y axis) rotation speed+direction.
The spring part of this video does a good job demonstrating and explaining what springs can do and how they work:
You should also check out realism, it has a lot of great stuff for what you’re doing and is really easy to use without needing to code additional stuff.