Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jzaun · Jun 26, 2014 at 04:57 PM · cameracamera-movementboundsorthographic

Keeping the camera in bounds

I have a 2D Orthographic camera. Its pointing at my background GameObject. The background has a Mesh and a MeshRenderer on it, its created by UniTMX. I would like to let the player drag the camera around but I want it to always have the background visible and covering the full screen.

This is what I'm trying... Its not really working at all. I'm guessing GameObject.Find("Background").GetComponent().sharedMesh.bounds isn;t what I should be looking at.

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
   enum Mode {Idle, Follow, Drag}
 
   private Bounds backgroundBounds;
   private float rightBound;
   private float leftBound;
   private float topBound;
   private float bottomBound;
   
   private Mode state = Mode.Idle;
 
   public Transform followTarget;
   private Vector3 pos;
 
   public float dragSpeed = 2;
   private Vector3 dragOrigin;
    
   void Start () 
   {
     float vertExtent = Camera.main.camera.orthographicSize;  
     float horzExtent = vertExtent * Screen.width / Screen.height;
  
     backgroundBounds = GameObject.Find("Background").GetComponent<MeshFilter>().sharedMesh.bounds;
     leftBound = (float)(horzExtent - backgroundBounds.size.x / 2.0f);
     rightBound = (float)(backgroundBounds.size.x / 2.0f - horzExtent);
     bottomBound = (float)(vertExtent - backgroundBounds.size.y / 2.0f);
     topBound = (float)(backgroundBounds.size.y  / 2.0f - vertExtent);
 
     Debug.Log(backgroundBounds);
   }
    
   void Update () 
   {
 
     // Switch Modes
     if (Input.GetMouseButtonDown(0)) {
       state = Mode.Drag;
       dragOrigin = Input.mousePosition;
       followTarget = null;
     }
     else if (!Input.GetMouseButton(0) && state == Mode.Drag) {
       state = Mode.Idle;
     }
     else if (followTarget && state == Mode.Idle) {
       state = Mode.Follow;
     }
 
     // Drag camera around
     if (state == Mode.Drag) {
       Vector3 mousePos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
       Vector3 move = new Vector3(mousePos.x * dragSpeed, mousePos.y * dragSpeed, 0);
       transform.Translate(move, Space.World); 
     }
 
     // If we have a target to follow move to them, but stay on the background
     else if (state == Mode.Follow) { 
       var pos = new Vector3(followTarget.position.x, followTarget.position.y, followTarget.position.z);
       transform.position = pos;
     }
   }
 
   void LateUpdate () {
     Vector3 v3 = transform.position;
     v3.x = Mathf.Clamp(v3.x, leftBound, rightBound);
     v3.y = Mathf.Clamp(v3.y, bottomBound, topBound);
     transform.position = v3;
   }
    
   void OnLevelWasLoaded ()
   {
     Start();
   }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Jun 26, 2014 at 04:58 PM

If you want the background to stay in a ixed position to the camera simply make it a child object of the camera object.

No code is needed.

(if you want a background that's bigger then the camera that you can scroll around, thats another story but then specify that in the question.)

Comment
Add comment · Show 2 · Share
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
avatar image jzaun · Jun 26, 2014 at 05:13 PM 0
Share

The background is larger than the camera view area.

avatar image Jeff-Kesselman · Jun 26, 2014 at 05:32 PM 0
Share

Okay thats a bit trickier. But lets assume you have the background centered at position 0,0,0

There are a couple ways to do it. You can do it in camera space, or you can do it in screen space. I think id use screen space because its easier to find the dimensions of then the camera frustrum.

In order to find the bounds of the texture in screen space, use http://docs.unity3d.com/ScriptReference/Camera.WorldToScreenPoint.html on the four corners of the texture in world space.

If any of those points are inside the screen rectangle, it means that you have scrolled too far.

You can fix it by subtracting the texture's corner from the corner of the screen it should be in, and using http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html to convert the delta back to a world measure to move the camera by.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Implementing Camera bounds proportionally to its ortographic size 0 Answers

Fitting Bounds into Orthographic 2d Camera. 2 Answers

Unity ortographic camera panning within boundaries issue 0 Answers

How do I lock an orthographic camera? 1 Answer

How do I keep an Ortho camera in a specific range when the ortho changes? 3 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