using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowPlayer : MonoBehaviour { private static GameObject player; public float boundsX = 0.0f; public float boundsY = 0.0f; // Start is called before the first frame update void Start() { player = GameObject.FindWithTag("Player"); if (player != null) { // Inherit player's X and Y, but not Z. transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z); } } // Update is called once per frame void Update() { // Center to player. if (player != null) { // Inherit player's X and Y, but not Z. float new_x = transform.position.x; new_x = Mathf.Max(new_x, player.transform.position.x - boundsX); new_x = Mathf.Min(new_x, player.transform.position.x + boundsX); float new_y = transform.position.y; new_y = Mathf.Max(new_y, player.transform.position.y - boundsY); new_y = Mathf.Min(new_y, player.transform.position.y + boundsY); transform.position = new Vector3(new_x, new_y, transform.position.z); } } }