- Home /
Clamping the Camera, according to the player ?
Hello everyone, I am trying to achieve a little different kind of a FPS camera. First, my camera is no the child of our player, they are seperated. When my player is standing still, I want to be able to look at the player body freely, without rotating the player. So I wrote my camera script according to that, camera is rotated through X & Y inputs. If the camera looks to right or left with 80~ angle, I want the character body to start rotating. I achieved this, I substract player's euler Y from camera's euler Y, and if that value is bigger than 80, I start rotating the player to the euler Y of the camera. ( I use Mathf.Abs to determine if it's bigger than 80 or not. ) Now the problem comes, If I rotate my camera too fast, player's body can't catch it and camera sometimes sees the back of the body, since there are no restrictions for it. So I need to clamp it, but simply clamping it with -90 & 90 is a huge problem, since camera has no parent object it's world euler y will be clamped and it will be unable to rotate more than -90, 90, according to world. So I need something like this :
f_InputY = Mathf.Clamp(f_InputY, t_Player.eulerAngles.y - 90, t_Player.eulerAngles.y + 90);
but when I use this, camera seems to jump. Because for example let's say player's euler Y is 0, if I rotate it left, it'll be 359. 359 - 90 = 269. So the camera will be clamped with 90 - 269, which makes it jumps to the 90 directly.(Where it's actually supposed to be in the region of -90, 90) So, any ideas how to implement a system like that ? I tried a lot of different things, like parenting the camera and setting the position & rotation of that parent equal to a reference child object's position & rotation, which is a child of the player. But I could not achieve a smooth controlling. Also some other problems occur when I move the character, because if the player is not standing still, I want the controls to switch to a casual FPS Camera, which X Input rotates the character, and Y Input rotates the camera only. And there are too many ways to achieve rotations, like using AngleAxis, SmoothDampAngle, Quaternion.Lerp, Slerp and more on. I do not know which one is suitable for which way of controlling. If anyone could point me in the right direction or give some guidelines, I'll very happy. Thanks :)