Hello. I want to make a camera border, some kind of camera analogue like in Mario or the like.
It was originally done like this. It was (video) (+ a couple of lessons earlier).
using UnityEngine; using System.Collections; public class CameraController : MonoBehaviour { public PlayerController player; [Header ("Привязка камеры")] public bool isFollowing; public float xOffset; public float yOffset; // Use this for initialization void Start () { player = FindObjectOfType<PlayerController>(); isFollowing = true; } // Update is called once per frame void Update () { if (isFollowing) transform.position = new Vector3 (player.transform.position.x+xOffset, player.transform.position.y+yOffset, transform.position.z); } }
I want (video) (+ maybe 1 lesson ahead)
using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { private Vector2 velocity; public float smoothTimeY; public float smoothTimeX; //public GameObject player; public PlayerController player; public bool bounds; public Vector3 minCameraPos; public Vector3 maxCameraPos; // Use this for initialization void Start () { //player = GameObject.FindGameObjectsWithTag ("Player"); player = FindObjectOfType<PlayerController>(); } // Update is called once per frame void FixedUpdate () { float posX = Mathf.SmoothDamp (transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX); float posY = Mathf.SmoothDamp (transform.position.y, player.transform.position.y, ref velocity.x, smoothTimeY); transform.position = new Vector3 (posX, posY, transform.position.z); if (bounds) { transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCameraPos.x, maxCameraPos.x), Mathf.Clamp(transform.position.y, minCameraPos.y, maxCameraPos.y), Mathf.Clamp(transform.position.z, minCameraPos.z, maxCameraPos.z)); } } }
I sat and understood, but there was some kind of conflict. I don’t notice something. Please tell me how to get out of this situation.
UPDATE : The code has earned, if you remove the activity from the first script and pull the camera out of the child objects of the “Player”, but now the hero's sprite disappears after death.