C# script to move camera after players or object moves needed amount of space.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// © 2017 TheFlyingKeyboard
// theflyingkeyboard.net

public class TileCameraController: MonoBehaviour {
    [SerializeField] private Transform target;
    [SerializeField] private Vector3 offset;

  void FixedUpdate () {
    if(Mathf.Abs(transform.position.x - target.position.x) > offset.x)
        {
            MoveCamera();
        }

        if (Mathf.Abs(transform.position.y - target.position.y) > offset.y)
        {
            MoveCamera();
        }
    }

    void MoveCamera()
    {
        transform.position = new Vector3(target.position.x, target.position.y, transform.position.z);
    }
}
Script used to controll object movement
Unity C# 2D Tile Camera Controller
Tagged on:         

Leave a Reply

Your email address will not be published. Required fields are marked *