A
A
Artyom2015-11-16 22:17:40
Game development
Artyom, 2015-11-16 22:17:40

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);
}
}

I want https://www.youtube.com/watch?v=I6xmOMsRWeo (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 is some kind of conflict. I don’t notice something. Please tell me how to get out of this situation.
The project itself. https://www.dropbox.com/s/l4hybkh95rsuejd/Lost%20P...
UPD.In advance.
The code worked if we removed the activity from the first script and pulled the camera from the child objects of the "Player", but now the hero's sprite disappears after death.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question