Hello and thanks for reading my thread! I am currently working on the camera system of my game (third person). Anyway, I used this code to adjust my camera position when its being tilt or moved:
public void CameraSync()
{
if (!_Errors)
{
// Calculate the camera rotation and position based on the
// Mouse X and Y movement
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + CameraPos.position;
_myTransform.position = position;
_myTransform.rotation = rotation;
}
}
I call this function once on start, once per frame. The player moves with another called CharacterMovements. This way the camera “acts” like parented to the player. Im rotating the camera when right mouse button is being held down:
if (Input.GetButton("Mouse RightClick"))
{
if (_CameraMode != "First Person")
{
Screen.showCursor = false;
Screen.lockCursor = true;
_x += Input.GetAxis("MouseX") * xSpeed * 0.02f;
_y -= Input.GetAxis("MouseY") * ySpeed * 0.02f;
}
All of the above works. I can rotate the camera any way I want with my mouse, the camera is following the invisible CameraPosition gameobject that is located a little bit above the players head.
The thing I’m trying to change is that when my player turns right by an angle _x around the Y axis, I want the camera to do the rotation at the same time (So the camera looks ‘locked’). I was using this code:
if (Input.GetButton("Turn"))
{
_x = _player.transform.rotation.eulerAngles.y;
}
Where _player is a cached version of the well player ^^ and transform.rotation refers to the camera rotation. This worked out too. Now my camera is always behind the player. Problems start when I try to rotate my camera angle with RightMouse … as expected. On start of the game for example both rotations are 0°. I turn the player for 90° to the right, the camera follows. But when I tilt the camera for 90° example and then move the player, it snaps right behind the players back again, instead to lock in the new position. Thats because my code above draws an equal sign between camera and player rotation.
I just don’t know how to fix it… I tried to add player and camera rotations up but that didn’t work either, the camera started to rotate like mad around the player.
Hope I explained everything good enough! If there is any other questions just ask.
Thanks in Regards ~Ilhan