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 Vladimir Krom · Feb 01, 2015 at 06:19 PM · camera-movementboundsorthographic

How do I lock an orthographic camera?

I made on orthographic camera with a rotation of 30 45 0 to imitate the isometric view. My goal is to make a 2.5D game like Farmville but i can't find any solution on how to lock the camera on left/right, up/down, I will put the code on how i move the camera:

  void Update () {
     if((Input.GetKey(KeyCode.LeftArrow)))
     {
         transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
     }
     if((Input.GetKey(KeyCode.RightArrow)))
     {
         transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
     }
     if((Input.GetKey(KeyCode.UpArrow)))
     {
         transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
     }
     if(Input.GetKey(KeyCode.DownArrow))
     {
         transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
     }
 }

I want to limit the camera on this box. alt text

dasf.jpg (60.3 kB)
Comment
Add comment · Show 2
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 AngryBurritoCoder · Feb 01, 2015 at 09:45 PM 0
Share

so you want to put like border limits to how far the camera can move left right top and bottom ? if i understood correctly

avatar image Vladimir Krom · Feb 02, 2015 at 12:11 PM 0
Share

yes, that is exactly what i need :D alt text

lock.jpg (23.8 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mr. Mud · Feb 01, 2015 at 10:32 PM

This small piece of code should keep the camera in the bounds you can define in the inspector. After you have moved the object, this code makes sure that the position on each axis is between the bounds (Mathf.Clamp).

Instead of using the Bounds class, you could also assign an object which defines bounds and get it from there; a Collider or a Renderer for example.

The function "OnDrawGizmos" is used to visualize the bounds that will be used by this script.


 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     [SerializeField]//Makes a private field show up in the inspector.
     private float cameraVelocity=0;
 
     [SerializeField]
     private Bounds bounds;
 
     public void Update () 
     {
         //Your code that is currently placed in Update.
 
         Bounds = bounds.bounds;
         Vector3 position = this.transform.position;
         position.x = Mathf.Clamp(position.x, bounds.min.x, bounds.max.x);
         position.y = Mathf.Clamp(position.y, bounds.min.y, bounds.max.y);
         position.z = Mathf.Clamp(position.z, bounds.min.z, bounds.max.z);
         this.transform.position = position;
     }
 
     public void OnDrawGizmos()
     {
         Gizmos.DrawWireCube(bounds.center, bounds.size);
     }
 }
Comment
Add comment · Show 5 · 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 Vladimir Krom · Feb 02, 2015 at 12:08 PM 0
Share

thanks for the reply, I've tried your code. It worked but I can't get it to work like I need to anyway it's been a great help, now I have a starting point. I'm still having problems on locking it to the cube's bounds. I have no ideea how to fix that :( .alt text

this is how i intend to lock the camera alt text

try.jpg (281.1 kB)
lock.jpg (23.8 kB)
avatar image Mr. Mud · Feb 02, 2015 at 01:12 PM 0
Share

After trying some things myself, I would suggest some slight alterations to the original code and the way the scene is setup. $$anonymous$$ake the camera a child of an empty GameObject with isometric rotation and set the rotation of the Camera to (0,0,0). This should basically look the same as it just did.

Then apply the following changes to your script. Now you need to tweak 4 values, opposed to 6. As for the settings, just tweak until you get it about right. $$anonymous$$y advice is that if you increase a value, you reduce the coresponding value by the same amount (for example: corner1.X +1; corner2.X -1). From here on it is mostly tweaking your values, something where I cannot really help. So I wish you best of luck.


 [SerializeField]//$$anonymous$$akes a private field show up in the inspector.
 private float cameraVelocity=0;
     
 [SerializeField]
 private Vector2 corner1, corner2;
 
 void Update () 
 {
     Vector3 position = this.transform.localPosition;
     if((Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)))
     {
         position.x -= cameraVelocity * Time.deltaTime;
     }
     if((Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)))
     {
         position.x += cameraVelocity * Time.deltaTime;
     }
     if((Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)))
     {
         position.y += cameraVelocity * Time.deltaTime;
     }
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
     {
         position.y -= cameraVelocity * Time.deltaTime;
     }
     position.x = $$anonymous$$athf.Clamp(position.x, corner1.x, corner2.x);
     position.y = $$anonymous$$athf.Clamp(position.y, corner1.y, corner2.y);
     this.transform.localPosition = position;
 }


avatar image Vladimir Krom · Feb 02, 2015 at 01:27 PM 0
Share

By making and empty gameobject you mean bind the camera with a rotation of 0, 0, 0 to it right? And add the script to the object not on the camera. I also need to rework everything to match my mouse control script and zoo$$anonymous$$g, anything that I should be aware of when doing that ?

avatar image Mr. Mud · Feb 02, 2015 at 01:35 PM 0
Share

Here is an image with the settings which I was talking about. It should hopefully make things clearer. alt text

example.png (76.8 kB)
avatar image Vladimir Krom · Feb 02, 2015 at 01:39 PM 0
Share

Oh, I should be able to make this work, thanks. $$anonymous$$y script is updating the position in the mouse if's but i will make just a variable update in the if and update the position in the end with $$anonymous$$athf.Clamp just like you told me. Thanks again. I will come with an update later on.

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

Keeping the camera in bounds 1 Answer

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

Limit camera from start to end of map sprite 0 Answers

Orthographic (Birds-Eye View/Top-Down) Player Follow Script w/ Smoothing 2 Answers

Implementing Camera bounds proportionally to its ortographic size 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