Camera controller. Log #7 (Unity, C#)
28.02.2018

          The player character moves, but the camera remains on position. There are going to be two ways how to control camera. First, camera will follow the character and second, when the player can move camera over the level. In folder Scripts create folder Camera and inside this folder create new script called CameraController. First I will deal with moving with camera (which is easier), then the following. Camera willc be moved by WASD or arrow keys, so its only about positioning the camera, and also setting the speed of motion.  The problem is, when I want to move camera forward od backward. The camera origin is rotated (along with camera of course) to follow the player, so the camera will move in direction of its (local) origin. So I need to store the current camera rotation, then I reset axis X and Z, then I move with the camera and finally I set the previous rotation. This will happen in every frame, so human eye won't even notice and camera will move smoothly. I put all of it in function Move() called only when camera don't follow the player character.

 

void Move()
{
	Vector3 rot = transform.rotation.eulerAngles;
	transform.rotation = Quaternion.Euler(0, rot.y, 0);
	float xAxisValue = Input.GetAxis("Horizontal");
	float zAxisValue = Input.GetAxis("Vertical");
	transform.Translate(new Vector3(xAxisValue * cameraSpeed, 0.0f, zAxisValue * 	cameraSpeed));
	transform.rotation = Quaternion.Euler(rot);
}

 

          Now following the player. The camera will follow the character with some distance, this is going to be an offset. In this distance will camera rotate around the character, but also follow the character with constant offset. An offset is 3D vector telling me how far will camera be along the axis X, Y and Z. Y is height and Z is forward/backward distance, I don't use X axis because it's movement to the right or left, which is undesirable. Important element is function LookAt(), ensuring, that camera will always look at my character. That's not enought though, camera need to follow the character when it moves. That's quite simple, because we just copy the position of the character and add our offset to it. So at first I need to store my rotation (which is only position of camera on the circle around the character). By pressing A/D keys I just change the camera position around the character, this position needs to by multiplied by my offset. In this case may halp function Quaternion.AngleAxis, which converts an angle to coordinates. I add the final position of camera  to the currnet offset. At the end I call function LookAt(). When I want to change vertical position of camera I need to change Y axis in current offset, by pressing W/S keys.

 

void Update()
{
	if (follow)
	{
		offset = Quaternion.AngleAxis(-Input.GetAxis("Horizontal") * sensitivityRotation, Vector3.up) * offset;

		offsetY += (Input.GetAxisRaw("Vertical") * sensitivityVertical);

		if (offsetY < maxYDistance && offsetY > minYDistance)
		{
			offset.y = offsetY;
		}

		if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S))
		{
			offsetY = offset.y;
		}

		transform.position = target.position + offset;
		transform.LookAt(target);

		}
		else
		{
			Move();
		}

}