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 DevMerlin · Jun 16, 2020 at 06:54 PM · camerabounds

This camera controller is overriding it's default position when the boundary check is enabled?

I'm working on a camera controller for a 2D top-down grid. This works almost perfectly, -however-, I've discovered a discrepancy in that it's boundary check resets it's default position to zero, and ignores whatever position the camera is assigned at first.

I've been trying to figure this out, but I have been stumped. The problem is occurring in LateUpdate, and any position the camera starts with will revert to X = 0, Y = height, Z = 0.

How can I correct this?


 using UnityEngine;
 
 /// <summary>
 /// Top Down Camera Script - Orthographic View
 /// <para>Sets up a top down camera, with optional zooming, and controlled by the horizontal and veritcal axis.</para>
 /// <para>Designed for free scrolling of a map or set defined area.</para>
 /// </summary>
 public class CameraController : MonoBehaviour
 {
     public Bounds visibleBounds;
     public Transform boundsMarker;
 
     public float activeSpeed = 0;
     public float fastMulti = 0.5f;
     public float keyScroll = 0.25f;
 
     public float zoomMax = 25;
     public float zoomMin = 50;
 
     public float zoomControl = 0;
     public float zoomSensitivity = 5f;
 
     public bool zoomEnabled = true;
     public bool cameraReady = false;
     public bool clampBounds = false;
 
     Vector2 cameraSizeWorld;
     Vector2 halfSize;
 
     private Camera _camera;
     private AudioListener _listener;
     private Transform _trans;
 
     private void Awake()
     {
         _trans = GetComponent<Transform>();
         _camera = GetComponent<Camera>();
         _listener = GetComponent<AudioListener>();
     }
 
     public void setCamEnabledState(bool state)
     {
         _camera.enabled = state;
         _listener.enabled = state;
     }
 
 
     void FixedUpdate()
     {
         if (cameraReady)
         {
             CheckKeyboardScroll();
 
             if (zoomEnabled)
             {
                 checkZoomState();
             }
 
             updateExtents();
 
             Debug.Log("Position at Update: " + _trans.position);
         }
     }
 
 
     /// <summary>
     /// <para>Listen for the horizontal and vertical camera axis.</para>
     /// <para>This is designed to be used on a free map, not following a target.</para>
     /// </summary>
     private void CheckKeyboardScroll()
     {
         if (!Input.GetKey(KeyCode.LeftShift))
         {
             activeSpeed = keyScroll;
         } else
         {
             activeSpeed = fastMulti;
         }
 
         float _x = Input.GetAxis("Horizontal") * activeSpeed;
         float _z = Input.GetAxis("Vertical") * activeSpeed;
 
         _trans.position = _trans.position + new Vector3(_x * Time.deltaTime, 0, _z * Time.deltaTime);
     }
 
     /// <summary>
     /// Position camera to fit the grid size, using orthographic value to zoom appropriately.
     /// </summary>
     /// <param name="boardSize"></param>
     public void positionCamera(float boardSize)
     {
 
         if (_camera)
         {
             Vector3 pos = _trans.position;
             pos.z = -0.90f;
             _camera.orthographicSize = (boardSize * Screen.height / Screen.width * 0.5f) + 1.85f;
             _trans.position = pos;
         }
 
         cameraReady = true;
     }
 
     /// <summary>
     /// Check if the resolution of the camera has changed
     /// </summary>
     private void updateExtents()
     {
         if (boundsMarker)
         {
             visibleBounds = boundsMarker.GetComponent<BoxCollider>().bounds;
             cameraSizeWorld = _camera.ViewportToWorldPoint(Vector2.one) - _camera.ViewportToWorldPoint(Vector2.zero);
             halfSize = new Vector2(Mathf.Floor(cameraSizeWorld.x / 2), Mathf.Floor(cameraSizeWorld.y / 2));
         }
     }
     /// <summary>
     /// <para>If zoom controls are enabled, allow zooming the orthographic size.</para>
     /// </summary>
     private void checkZoomState()
     {
         zoomControl = _camera.orthographicSize;
         zoomControl -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity;
         zoomControl = Mathf.Clamp(zoomControl, zoomMin, zoomMax);
         _camera.orthographicSize = zoomControl;
     }
 
     /// <summary>
     /// Keep the camera clamped to a certain map range, limiting the area that can be moved in.
     /// </summary>
     private void LateUpdate()
     {
         if (boundsMarker && clampBounds)
         {
             Vector3 v3 = transform.position;
             v3.x = Mathf.Clamp(v3.x, visibleBounds.min.x + halfSize.x, visibleBounds.max.x - halfSize.x);
             v3.z = Mathf.Clamp(v3.z, visibleBounds.min.z + halfSize.y, visibleBounds.max.z - halfSize.y);
             transform.position = v3;
         }
     }
 }

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

193 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 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

map boundaries for perspective camera 1 Answer

Ease in transform.LookAt() - keep other target within camera boundaries 1 Answer

TestPlanesAABB Does not work when looking up. -1 Answers

3D object space occupied in camera 0 Answers

Find camera bounds at certain distance from camera? 1 Answer


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