> For the complete documentation index, see [llms.txt](https://kdoore.gitbook.io/cs2335/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdoore.gitbook.io/cs2335/master_v2/project-3-code-mods/project-3-code/class-code-examples/camerafollow.md).

# CameraFollow

This script can be attached to the mainCamera to have the camera follow the player if there are scrolling images.

From OReilly:  Learning 2D Game Development with Unity by Matthew Johnson:   Section 11.7  The author say this script is a Unity Script.  &#x20;

Also, you may want to try setting the Player's RigidBody2D Interpolate to Interpolate, as per discussions below

{% embed url="<https://answers.unity.com/questions/29183/2d-camera-smooth-follow.html>" %}

{% embed url="<https://docs.unity3d.com/ScriptReference/Rigidbody-interpolation.html>" %}

```java
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.
public class CameraFollow : MonoBehaviour
{
    public float xMargin = 1;
    public float yMargin = 1;
    public float xSmooth = 4;
    public float ySmooth = 4;
    public Vector2 maxXAndY; //try values: 35, 15
    public Vector2 minXAndY; //try values: -1,0

    private Transform player;

    void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    bool CheckXMargin()
    {
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }

    bool CheckYMargin()
    {
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }

    void FixedUpdate()
    {
        TrackPlayer();
    }

    //set camera to default position
    public void ResetPosition()
    {
        transform.position = new Vector3(0, 0, -10);
    }

    void TrackPlayer()
    {
        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 = new Vector3(targetX, targetY,
    transform.position.z);
    }
} //end class
```
