using UnityEngine;//https://learning.oreilly.com/library/view/learning-2d-game/9780133523416/ch11lev2sec19.html#ch11lev2sec19//this script can be added to the camera in a 2D game so the camera follows the player//with smoothed following when the player nears the margins of the camera's view.publicclassCameraFollow : MonoBehaviour{publicfloat xMargin =1;publicfloat yMargin =1;publicfloat xSmooth =4;publicfloat ySmooth =4;publicVector2 maxXAndY; //try values: 35, 15publicVector2 minXAndY; //try values: -1,0privateTransform player;voidAwake() { player =GameObject.FindGameObjectWithTag("Player").transform; } bool CheckXMargin() {returnMathf.Abs(transform.position.x-player.position.x) > xMargin; } bool CheckYMargin() {returnMathf.Abs(transform.position.y-player.position.y) > yMargin; }voidFixedUpdate() {TrackPlayer(); }//set camera to default positionpublicvoidResetPosition() {transform.position=newVector3(0,0,-10); }voidTrackPlayer() {float targetX =transform.position.x;float targetY =transform.position.y;if (CheckXMargin()==true) { targetX =Mathf.Lerp(transform.position.x,player.position.x, xSmooth *Time.deltaTime); }if (CheckYMargin()==true) { targetY =Mathf.Lerp(transform.position.y,player.position.y, ySmooth *Time.deltaTime); } targetX =Mathf.Clamp(targetX,minXAndY.x,maxXAndY.x); targetY =Mathf.Clamp(targetY,minXAndY.y,maxXAndY.y);transform.position=newVector3(targetX, targetY,transform.position.z); }} //end class