Make the camera follow player rotation

using UnityEngine;
using System.Collections;

public class DoneCameraMovement : MonoBehaviour

public float smooth = 2.5f;			// The relative speed at which the camera will catch up.

private Transform player;			// Reference to the player's transform.
private Vector3 relCameraPos;		// The relative position of the camera from the player.
private float relCameraPosMag;		// The distance of the camera from the player.
private Vector3 newPos;				// The position the camera is trying to reach.

void Awake ()
	// Setting up the reference.
	player = GameObject.FindGameObjectWithTag(DoneTags.player).transform;
	// Setting the relative position as the initial relative position of the camera in the scene.
	relCameraPos = transform.position - player.position;
	relCameraPosMag = relCameraPos.magnitude - 0.5f;

void FixedUpdate ()

	// The standard position of the camera is the relative position of the camera from the player.
	Vector3 standardPos = player.position + relCameraPos;
	// The abovePos is directly above the player at the same distance as the standard position.
	Vector3 abovePos = player.position + Vector3.up * relCameraPosMag;
	// An array of 5 points to check if the camera can see the player.
	Vector3[] checkPoints = new Vector3[5];
	// The first is the standard position of the camera.
	checkPoints[0] = standardPos;
	// The next three are 25%, 50% and 75% of the distance between the standard position and abovePos.
	checkPoints[1] = Vector3.Lerp(standardPos, abovePos, 0.25f);
	checkPoints[2] = Vector3.Lerp(standardPos, abovePos, 0.5f);
	checkPoints[3] = Vector3.Lerp(standardPos, abovePos, 0.75f);
	// The last is the abovePos.
	checkPoints[4] = abovePos;
	// Run through the check points...
	for(int i = 0; i < checkPoints.Length; i++)
		// ... if the camera can see the player...
  •   		// ... break from the loop.*
  •   		break;*
  •   }*
  •   // Lerp the camera's position between it's current position and it's new position.*

_ transform.position = Vector3.Lerp(transform.position, newPos, smooth * Time.deltaTime);_

  •   // Make sure the camera is looking at the player.*
  •   SmoothLookAt();*
  •   SmoothRotation();*
  • }*

  • bool ViewingPosCheck (Vector3 checkPos)*

  • {*

  •   RaycastHit hit;*
  •   // If a raycast from the check position to the player hits something...*
  •   if(Physics.Raycast(checkPos, player.position - checkPos, out hit, relCameraPosMag))*
  •   	// ... if it is not the player...*
  •   	if(hit.transform != player)*
  •   		// This position isn't appropriate.*
  •   		return false;*
  •   // If we haven't hit anything or we've hit the player, this is an appropriate position.*
  •   newPos = checkPos;*
  •   return true;*
  • }*

  • void SmoothLookAt ()*

  • {*

  •   // Create a vector from the camera towards the player.*
  •   Vector3 relPlayerPosition = player.position - transform.position;*
  •   // Create a rotation based on the relative position of the player being the forward vector.*
  •   Quaternion lookAtRotation = Quaternion.LookRotation(relPlayerPosition, Vector3.up);*
  •   // Lerp the camera's rotation between it's current rotation and the rotation that looks at the player.*

_ transform.rotation = Quaternion.Lerp(transform.rotation, lookAtRotation, smooth * Time.deltaTime);_

  • }*
    The camera currently follows the player with a fixed rotation.
    I cant figure out how to make the camera also follow the player rotation. He moves as rotates with WASD.
    Can anyone please help me?

Why not just mirror the rotation of the players gameObject? As long as you always want the camera to have basically the same viewpoint as when you place it you should be able to just apply the characters rotation as the cameras rotation.

Import Scripts from assets …here you will find smooth follow script in standard assets folder. I am copying the script here

Just store your player as target and your add the script to your camera. you will see that your camera will follow the player rotation

/* This camera smoothes out rotation around the y-axis and height. Horizontal Distance to the target is always fixed.

There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

For every of those smoothed values we calculate the wanted value and the current value. Then we smooth it using the Lerp function. Then we apply the smoothed values to the transform’s position. */

// The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0;

// Place the script in the Camera-Control group in the component menu @script AddComponentMenu(“Camera-Control/Smooth Follow”)

function LateUpdate () { // Early out if we don’t have a target if (!target) return;

// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;

var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;

// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;

// Set the height of the camera
transform.position.y = currentHeight;

// Always look at the target
transform.LookAt (target);