Answer the question
In order to leave comments, you need to log in
How to make camera borders?
Hello. I wanted to make the borders of the camera, some analogue of the camera like in Mario or the like.
It was originally done like this. It was https://www.youtube.com/watch?v=8aVZuL9ocrk (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);
}
}
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));
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question