简体   繁体   中英

How to stop camera from moving further than the map?

So i have a 2D world in Unity which has a sprite (Some hills). That is the map. I also have a camera that moves left - right when the mouse reaches the borders of the game's window using the following script (I hope it's not too messy, i'm lazy today:/, i will add some comments to help).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveCam : MonoBehaviour
{
    public Camera Camera; //idk why i did this the component is parented to maincam
    public float Divider; //This is to reduce the move amount to reasonable numbers.
    void Start()
    {
         
    }

    void FixedUpdate()
    {
        Vector3 mousePos = Input.mousePosition; //The simple stuff. Getting the mouse position

        Debug.Log(mousePos.x); //Some debug stuff
        Debug.Log(mousePos.y);
        Debug.Log(Screen.width / 14);
        Debug.Log(Screen.width - (Screen.width / 14));
        
        if (mousePos.x < Screen.width / 14) //The if statement to trigger the movement only when the mouse is in the calculated border radius. I use the example   49 < 700 / 14 = 49 < 50
        {
            Camera.transform.Translate((mousePos.x - Screen.width / 14) / Divider, 0, 0); //Move the camera to the left by the calculated strength which is the distance between the mouse and the border radius end reduced to reasonable amounts by the Divider variable. It is set to 50 in the engine currently. The example is   (49 - 700 / 14) / 50 = (49 - 50) / 50 = -1 / 50 = -0.02
            Debug.Log((mousePos.x - Screen.width / 14) / Divider); //Debug stuff
        } else if (mousePos.x > Screen.width - (Screen.width / 14)) //If not, we'll check if the mouse is in the right. We calculate the right border radius by subtracting the full width of the game window by the calculated radius.   651 > 700 - (700 / 14) = 651 > 700 - 50 = 651 > 650
        {
            Camera.transform.Translate((mousePos.x - (Screen.width - (Screen.width / 14))) / Divider, 0, 0); //The same, but we replace the left with right using the algorithm that's explained above.
            Debug.Log((mousePos.x - (Screen.width - (Screen.width / 14))) / Divider); //DEBUGGGG
        }
    }
}

So that's the code. Now i want to add the logic for stopping the camera from extending further than the map. The problem is that i want the game to be responsive, so the set limitations are not an option. I had an idea that could possibly work exactly how i wanted, but it didn't work for some reason. When searching on the inte.net i found a solution, but it was confusing and unexplained, so i decided to make this post, I will be really thankful if you help! This is an image of the scene if needed.

I solved it by using Camera.ViewportToWorldPoint() and spriteRenderer.bounds .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM