0

Скрипт почти дописан опытным путем)), но остается один вопрос: Как получить эти же значения формулой, что бы в будущем приближать-отдалять и ничего не сбивалось? Это значения когда камера доходит до границы TileMap cameraPos on bounds = x2.4 y2 (при orthographicSize = 5)

using UnityEngine;

public class CameraController : MonoBehaviour
{
    Vector3 mousePos;
    GameManager gameManager;
    float boundX;
    float boundY;

    private void Start()
    {
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
        boundX = gameManager.gameMapSize.x - 1;
        boundY = gameManager.gameMapSize.y - 1;
    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            mousePos.x -= Input.GetAxis("Mouse X") / 2;
            if (mousePos.x > 2.4f)
            {
                mousePos.x += Input.GetAxis("Mouse X") / 2;
            }
            mousePos.y -= Input.GetAxis("Mouse Y") / 2;
            if (mousePos.y > 2f)
            {
                mousePos.y += Input.GetAxis("Mouse Y") / 2;
            }
            mousePos.z = transform.position.z;
            Debug.Log(mousePos.x + " " + mousePos.y);
            Camera.main.transform.position = mousePos;
        }
    }
    void ValidPos()
    {
        Camera cam = Camera.main;
        float height = 2f * cam.orthographicSize;
        float width = height * cam.aspect;
        //Height: 10
        //Width: 21.21
        //tilemapSize = x26, y14
        //cameraPos on bounds = x2.4 y2
    }
}

1 ответ 1

0

Отвечаю на свой вопрос - откуда цифры? 2.39f = (TilemapHeight - cameraHeight)/2 2f = (TIlemapWidth - cameraWidth)/2

using UnityEngine;

public class CameraController : MonoBehaviour
{
    Vector3 mousePos;
    GameManager gameManager;
    float boundX;
    float boundY;
    private void Start()
    {
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    }
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            GetTilemapBound();
            mousePos.x -= Input.GetAxis("Mouse X") / 2;
            if (mousePos.x > boundX || mousePos.x < -boundX)
            {
                mousePos.x += Input.GetAxis("Mouse X") / 2;
            }
            mousePos.y -= Input.GetAxis("Mouse Y") / 2;
            if (mousePos.y > boundY || mousePos.y < -boundY)
            {
                mousePos.y += Input.GetAxis("Mouse Y") / 2;
            }
            mousePos.z = transform.position.z;
            Camera.main.transform.position = mousePos;
        }
    }
    private void GetTilemapBound()
    {
        float mainCameraSize = Camera.main.orthographicSize;
        float height = 2f * mainCameraSize;
        float width = height * Camera.main.aspect;
        boundX = (gameManager.gameMapSize.x - width) / 2;
        boundY = (gameManager.gameMapSize.y - height) / 2;
    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.