Unity C#第三人称相机完整实现:自动定位+鼠标视角+视野缩放
Hey folks, I don't usually post here, but I've spent hours troubleshooting this camera issue and couldn't find any solutions online—so I wanted to share what I went through, especially for other Unity newbies like me.
What I Was Trying to Build
I was working on two core third-person camera behaviors:
- When the player moves, the camera automatically switches to a preset position and rotation (no mouse control here)
- When holding the right mouse button, enable mouse-controlled camera rotation—the camera follows mouse movement independently of where the player is positioned
The Initial Bug
The biggest issue I faced: After releasing the right mouse button, the camera appeared to snap back to the default view. But when I reactivated mouse control by holding right-click again, it would jump straight back to the position/rotation it was in when I last turned off mouse mode. This was really disorienting for players!
Troubleshooting & Optimization
I tried tons of code adjustments to fix this "stored rotation" problem, but nothing worked at first. Along the way, I also noticed my camera had an annoying jitter issue, so I tackled that too. After multiple rounds of updating and refining my code, I finally got everything working as intended.
Final Working Features
My camera system now does all this flawlessly:
- Smooth switching between auto-preset camera (during player movement) and mouse-controlled rotation (right-click hold)
- No more camera jitter
- Added mouse wheel control to zoom the camera in and out
内容的提问来源于stack exchange,提问作者Brel

