Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ivan_47 · Jan 19, 2017 at 05:14 PM · 2dmovementmapzoomrestrict

Movement and zoom of camera on the map

Hello, I'm trying to write a small 2d strategy, and I want to realize the movement of the camera with the mouse I have a pre-drawn map, which has a size 210x130 in Unity. I have a GameObject attachment with the camera, I move the GameObject the following script: using UnityEngine; using System.Collections;

 public class MoveCamera : MonoBehaviour {
 
 
     public float leftRestriction = -40f;
     public float rightRestriction = 10f;
     public float upRestriction = 30f;
     public float downRestriction = -10f;
 
     // Use this for initialization
     public int theScreenWidth;
     public int theScreenHeight;
     public int Boundary = -10;
 
     public int speed = 10;
     // Use this for initialization
     void Start ()
     {
         theScreenWidth = Screen.width;
         theScreenHeight = Screen.height;
 
     }
     
     // Update is called once per frame
     void Update ()
     {
 
         // Right
             if (Input.mousePosition.x > theScreenWidth - Boundary && transform.position.x <= rightRestriction)
         {
             float posX = transform.position.x;
             posX += speed * Time.deltaTime;
             transform.position = new Vector3(posX, transform.position.y, transform.position.z);
         }
         //Left
         if (Input.mousePosition.x < 0 + Boundary && transform.position.x >= leftRestriction)
         {
             float posX = transform.position.x;
             posX -= speed * Time.deltaTime;
             transform.position = new Vector3(posX, transform.position.y, transform.position.z);
         }
 
         //Up
         if (Input.mousePosition.y > theScreenHeight - Boundary && transform.position.y <= upRestriction)
         {
             float posY = transform.position.y;
             posY += speed * Time.deltaTime;
             transform.position = new Vector3(transform.position.x, posY, transform.position.z);
         }
 
         //Down
         if (Input.mousePosition.y < 0 + Boundary && transform.position.y >= downRestriction)
         {
             float posY = transform.position.y;
             posY -= speed * Time.deltaTime;
             transform.position = new Vector3(transform.position.x, posY, transform.position.z);
         }
     }
 }
 

Everything works fine but if I change the size of the camera, I can see the blue area. I worked with a zoom camera like this:

 using UnityEngine;
 using System.Collections;
 
 public class ZoomCamera : MonoBehaviour {
 
     public float zoomSpeed = 1;
     public float targetOrtho;
     public float smoothSpeed = 2.0f;
     public float minOrtho = 1.0f;
     public float maxOrtho = 20.0f;
     // Use this for initialization
     void Start()
     {
         targetOrtho = Camera.main.orthographicSize;
     }
 
     // Update is called once per frame
     public float lastOrtho;
     void Update()
     {
         float scroll = Input.GetAxis("Mouse ScrollWheel");
         if (scroll != 0.0f)
         {
             targetOrtho -= scroll * zoomSpeed;
             targetOrtho = Mathf.Clamp(targetOrtho, minOrtho, maxOrtho);
         }
 
         Camera.main.orthographicSize = Mathf.MoveTowards(Camera.main.orthographicSize,          targetOrtho, smoothSpeed * Time.deltaTime);
     }
 
 
     public GameObject MoveCamera;
 
 }
 

Could you tell me how to do so that when i change the size there is not the blue area. Maybe it is necessary to shift the camera with certain conditions, but I don't understand what conditions and how. I beg your help

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to develope RTS Map ? 2 Answers

How to create own 2d map with roads and moving cars. 0 Answers

Move 2D object forward 1 Answer

Adding small acceleration to 2d top down movement game? 0 Answers

How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges