camera movement

hello guys! how are you ? :slight_smile:
well i’ve been following a tutorial and I don’t understand the script
the target is to make the camera follow the player in a smooth way plus to not allow the camera to go off screen when the player falls off .
ok so i understand that we have a low y to prevent the camera from going down any further
but what i do not understand is the use of offset
ok so if i think about it
offset = camera position - player position
then
targetposition = player position + offset

if i think about it then target position = camera
so what’s the point of all this?

ok let’s say camera is (0,0,0)
and player is (1,1,0)
offset is (-1,-1,0)
target position is now (0,0,0)
when i say :

transform.position = Vector3.Lerp (transform.position, targetposition, smoothing*Time.deltaTime)

so camera’s new position is between the camera and the camera
please help this is so confusing me …
use examples and easy english if possible
here is the script below:
thank you so much in advance! :slight_smile:

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

public Transform playerposition;
public float smoothing;
private Vector3 offset;
private float lowy;

// Use this for initialization
void Start () {

	offset = transform.position - playerposition.position;
	lowy = transform.position.y;

}

// Update is called once per frame
void FixedUpdate () {

	Vector3 targetposition = playerposition.position + offset;

	transform.position = Vector3.Lerp (transform.position, targetposition, smoothing*Time.deltaTime);
	if (transform.position.y < lowy)
		transform.position = new Vector3 (transform.position.x, lowy, transform.position.z);
		}

}

You’re treating lines of code like math expressions. You can’t just replace lines from different blocks of code and turn additions in substractions.

In Start offest is assigned a fixed value, the difference between the transform.position (of the camera at the start of the scene) and the player position. It won’t change unless you assign a new value.

In Update, playerposition.position will have the current position of the player, updated whenever the player moves, but offset will have the original value, so targetposition will always be the player position plus the original camera position minus the the original player position.