Questions about the Camera Movement Script in the Stealth Tutorial

Hi. I’ll get right to the questions (there are two of them):

First Question

void Awake()
{
player = GameObject.FindGameObjectWithTag(Tags.player).transform;

relCameraPos = transform.position - player.position;
relCameraPosMag = relCameraPos.magnitude - 0.5f;
}


For the code that is written in bold:
We shorten the relCameraPos.magnitude by .5f. In the tutorial, they said that it was because the transform position is taken from the feet of the character, and we don’t want this.

Why don’t we want this? Is it because when we need to raycast to the player to see if there is a wall in the way we’d be raycasting all the way to his feet and this is bad?

Second Question

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

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


In both of the bolded instances, why do we not simply use the player’s position? What benefit does taking a relative vector give?

Thank you for your time, and if this question has already been answered (I didn’t see it on Unity answers or the forums) please point me in the direction of that thread.

Also, I forgot to ask:

I’m new to these forums. Is this question more appropriate in the answers section?

The last two bold lines are vector math. a position vector is going from the coordinate origin to the position. if you substract position vector a from position vector b you get a vector pointing from a to b. this vector is the position of b relative to a
Line 2: the want the players position relative to the transforms position.
Line 3: the first racast parameter is the origin and the second parameter is the direction. they want to shoot the ray from checkpos to the player so they use the vector pointing fromcheckpos to playerposition for the direction.

my guess on your first question is that usually you place your pivotpoints on imported charachters and objects by their feet so that when you are going to line them upp with the “world floor” it will become much easier. That also makes the objects local origin at their feet.

The camera will follow the players feet and not thair main body (im assuming the character in the game is 1 in height so they bumped upp the camera 0.5) this way the character will be in the main focus and if you want to zoom in on your charachter it will zoom in on the main body and not on its feet.

Raycasting will also be easier if you are focusing on that our target will be hiding behind stuff cause you shoot the ray onto his main body and not on his feet.

but this is only a guess.

Thank you for your responses! I had a fundamental misunderstanding of vectors in general, these answers really helped clear it up. I went and did some review; everything makes sense now. Thank you!