# 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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kdoore.gitbook.io/cs2335/master_v2/project-3-code-mods/project-3-code/class-code-examples/camerafollow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
