hello guys! how are you ?
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!
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);
}
}