# A 2D Camera Follow Script That Does Not Rotate?

I am attempting to make a 2D racing game. So far I have a player car (sprite) and a main camera.

I would like to know how to make the main camera follow the player car while still showing signs of movement (I don't want the positions to match exactly in the middle of the screen).

My Player Car has a Rigidbody2D and a MoveCar script

using UnityEngine;
using System.Collections;

public class MoveCar : MonoBehaviour {

public float speed = 10f;
public float gravity = 0f;

void CarMove ()
{
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
}

// Update is called once per frame
void Update ()
{
CarMove();
}
}


Then there is my Main Camera, its projection is switched to Orthographic and has default settings as well as a FollowCar script:

using UnityEngine;
using System.Collections;

public class FollowCar : MonoBehaviour {

public Transform player;

public GameObject other;
public Rigidbody2D rb = other.GetComponent<Rigidbody2D>;

void Update()
{
transform.Translate(Vector2.right * .5f * Time.deltaTime);
Vector2.MoveTowards( Vector2 , Vector2 player,
}
}


I would like to know where to go next or if I'm even going in the right direction. I have no idea how to utilize the MoveTowards() function.

There are lots of ways to do this, though MoveTowards seems reasonable for your purposes.

Essentially, you'll want to determine some speed you want the camera to follow the car. Let's say that's float cameraFollowSpeed;

Then you need to adjust that to be framerate independent with Time.deltaTime

So now FollowCar's Update() your code is something like

transform.position = Vector2.MoveTowards(
transform.position, car.transform.position,
Time.deltaTime*cameraFollowSpeed);


I don't think you need to use transform.Translate().

As written, with the appropriate (experimentally determined) value of cameraFollowSpeed, your camera should drift lazily behind the car.

This might be enough to feel fun, or you might want to calculate the cameraFollowSpeed each frame based on the distance between the camera and car.

Sounds to me like what you need is to define a box in the middle of the screen, and only if the car moves outside of that box then the camera will move. That way the car has a bit of freedom to move without the camera moving, but as soon as it gets too far away from the center of the screen the camera does follow it.

This is similar to the technique used in older platform games like Super Mario, where the camera would only move once mario gets near the right edge of the screen.

I've actually written a script like this in the last game jam so I can share it:

using UnityEngine;
using System.Collections;

public class CameraTracking : MonoBehaviour {

public Vector2 cameraSize;

public Vector2 horizontalWorldBounds;
public Vector2 verticalWorldBounds;

public GameObject player;

// Use this for initialization
private void Start()
{
transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z);
player = PlayerController.Instance.gameObject;
}

// Update is called once per frame
void Update ()
{
float deltaX = player.transform.position.x - transform.position.x;
deltaX = (Mathf.Abs(deltaX) > cameraSize.x) ? (Mathf.Abs(deltaX) - cameraSize.x) * Mathf.Abs(deltaX) / deltaX : 0;

float deltaY = player.transform.position.y - transform.position.y;
deltaY = (Mathf.Abs(deltaY) > cameraSize.y) ? (Mathf.Abs(deltaY) - cameraSize.y) * Mathf.Abs(deltaY) / deltaY : 0;

float width = CalculateGameWorldWidth();
float newX = Mathf.Clamp(transform.position.x + deltaX, horizontalWorldBounds.x + 0.5f * width, horizontalWorldBounds.y - 0.5f * width);
float newY = Mathf.Clamp(transform.position.y + deltaY, verticalWorldBounds.x + 0.5f * width, verticalWorldBounds.y - 0.5f * camera.orthographicSize);
transform.position = new Vector3(newX, newY, transform.position.z);
}

private float CalculateGameWorldWidth()
{
return (camera.pixelWidth / camera.pixelHeight) * camera.orthographicSize;
}
}


Attach this script to your camera. Assign the player car in "player" in the inspector.

cameraSize defines the size of the box in the middle of the screen where the player can move freely and the camera will stay put.

horizontalWorldBounds and verticalWorldBounds define the final bounds of your world, so the camera will never display anything outside these bounds even if the player moves there. It's the edges of the map. They are vector2, so x is the lower bound and y is the upper bound. These are in world coordinates.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.15407459437847137, "perplexity": 3501.0608549201647}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-29/segments/1593655886516.43/warc/CC-MAIN-20200704170556-20200704200556-00493.warc.gz"}