Stealth Project Tutorial Camera Movement

I have copy-pasted the code but still camera movement is not working as expected. It just rotates smoothly from the initial scene start towards initial position of the character and just stays looking from top-bottom and without following Ethan.

I am not getting any warnings or errors. What I might have missed? I think there is nothing much to be done to the main_camera game object apart from attaching the CameraMovement code attached to it. Please help

Update:

I have started from scratch again and re done the entire exercise again and got the same problem. This time I tried to debug the code with debug statements and I found interesting thing. What I did is that tried to trace the player position and camera position and what I have noticed is that player position doesn’t change at all

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;

		Debug.Log ("Camera Movement:" + standardPos.ToString() + " -- " + abovePos.ToString());

Output of debug statment

Camera Movement:(-5.2, 8.1, -4.7) – (0.0, 10.2, 0.0)

I was expecting the standardPos which is the player’s position (standardPos) will be -2.5,0,0 (which is initial position of ethan) and abovePos will be relative to the initial position of ethean. To my surprise standard position is showing (0,10.2,0) and the values doesnt change even when I move the player (though it moves in the game)

Also, when I traced player position in the Awake() function it shows (0,0,0) instead of expected (-2.5,0,0).

What might the problem? the code referencing the player variable to something different object instead of ethen? How do I find it?

Below is the copy - pasted code (without debug statements)

using UnityEngine;
using System.Collections;

public class CameraMovement : MonoBehaviour
{
    public float smooth = 1.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(Tags.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...
            if(ViewingPosCheck(checkPoints*))*

// … 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();
}

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);
}
}

I had the same problem. I found, that there were more objects with tag Player. The bad ones were “char_ethan (Clone)” and their Layer was 31 … the correct was only “char_ethan” in layer 0. Problem was solved by restarting Unity.

restart unity that did the trick for me.seems to me like unity goes wack sometimes.
the attached scripts with references to and dependent on position vectors just stop working especially the vectors it is more prominent if u r using a a DX10 GPU on a DX11 OS

Atleast i think this is the problem and solution

had similar problem. Restarting Unity just helped.