Camera limit Map

Hey Guys,
i need help. I wanna create a RTS topdown camera. I’ve got much already finished, but i cant get the camera limit .
i know it has something to do with mathf ,clamp etc.
but i dont know how to add it.
in general
i wanna limit the camera, so that you cant move endlessly away from the map.

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

public class CameraController : MonoBehaviour
{


    // Start is called before the first frame update



    public float moveSpeed;
    public float zoomSpeed;
    public float leftLimit;
   public  float rightLimit;
   public  float bottomLimit;
   public  float topLimit;
    public float minZoomDist;
    public float maxZoomDist;

    public float dragSpeed = 0.1f;
    private Vector3 dragOrigin;

    private Camera cam;
    // Update is called once per frame
     
                      

    private void Awake()
    {
        cam = Camera.main;

    }
    void Update()
    {
        Move();
        Zoom();
        Limit();

        if (Input.GetMouseButtonDown(1))
        {
            dragOrigin = Input.mousePosition;
            return;
        }

        if (!Input.GetMouseButton(1)) return;

        Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
        Vector3 move = new Vector3(pos.x * dragSpeed, 0, pos.y * dragSpeed);

        transform.Translate(move, Space.World);


    }

    void Move()
    {
        float xInput = Input.GetAxis("Horizontal");
        float zInput = Input.GetAxis("Vertical");

        Vector3 dir = transform.forward * zInput + transform.right * xInput;

        transform.position += dir * moveSpeed * Time.deltaTime;


    }
    void Limit()
    {
        transform.position = new Vector3
                 (
       Mathf.Clamp(transform.position.x, leftLimit, rightLimit),
        Mathf.Clamp(transform.position.y, bottomLimit, topLimit),
        transform.position.z

            );
    }
    void Zoom()

    {
        float scrollInput = Input.GetAxis("Mouse ScrollWheel");
        float dist = Vector3.Distance(transform.position, cam.transform.position);

        if (dist < minZoomDist && scrollInput > 0.0f)
            return;
        else if (dist > maxZoomDist && scrollInput < 0.0f)
            return;

        cam.transform.position += cam.transform.forward * scrollInput * zoomSpeed;

    }


 

}

For left and right it is working but not for up and down…

Nobody wants to read code as plain text. Please use code-tags when posting code. You can edit your post.

thx for that hint. any idea for my code?

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

If you want to debug what you have going on above, staring at code is not the proper way.

You actually need to reason about the values of the data involved.

This means you must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

You appear to be moving around the x/z plane but clamping the x/y.