Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 AmirLevi · Nov 22, 2018 at 11:07 AM · cameracamera-movementrtscamera-look

clamping camera movement to map size and camera corners

I'm working on RTS camera movement, and I'm trying to clamp its movement base on the map size and the corners of the camera.

alt text

this is my code:

 public void Init(Vector2 mapSize)
     {
 
         _camera = GetComponent<Camera>();
 
 
         _movePosition = transform.position =
             new Vector3(CameraStartPosition.x, CameraHeight, CameraStartPosition.z);
 
         var frustumHeight = 2.0f * CameraHeight * Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
         var frustumWidth = frustumHeight * _camera.aspect;
 
         var floorZSize = mapSize.y * _mapScaleFactor;
         var angle = Mathf.Sin(transform.localRotation.eulerAngles.x * Mathf.Deg2Rad);
         var distance = frustumHeight * 0.5f / Mathf.Tan(_camera.fieldOfView * 0.5f * Mathf.Deg2Rad);
         var hypotenuse = distance / angle;
         var adjacent = Mathf.Sqrt(Mathf.Pow(hypotenuse,2) - Mathf.Pow(distance,2));
         var zMax = floorZSize - adjacent ;
         var zMin = floorZSize + adjacent ;
         Debug.Log(
             $"frustumWidth: {frustumWidth} frustumHeight: {frustumHeight} " +
             $"filed of view: {_camera.fieldOfView} distance: {distance} " +
             $"mapHeight {floorZSize} angle{angle} hypotenuse: {hypotenuse} adjacent: {adjacent} zMax: {zMax} zMin: {zMin}");
 
         BoundsMin = new Vector3(
             _mapScaleFactor*(-mapSize.x) + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
             CameraHeight,
            -zMin);
 
         BoundsMax = new Vector3(
             _mapScaleFactor*mapSize.x + CameraHeight*Mathf.Sin(transform.localRotation.eulerAngles.z*Mathf.Deg2Rad),
             CameraHeight,
             zMax );
      }
 
 
 
     private void Update()
     {
         if (_camera == null) return;
 
         if (Input.GetKey(_moveUp.Key))
         {
             _movePosition.z += MovementSpeed * Time.deltaTime;
         }
 
         if (Input.GetKey(_moveRight.Key))
         {
             _movePosition.x += MovementSpeed * Time.deltaTime;
         }
 
         if (Input.GetKey(_moveDown.Key))
         {
             _movePosition.z -= MovementSpeed * Time.deltaTime;
         }
 
         if (Input.GetKey(_movLeft.Key))
         {
             _movePosition.x -= MovementSpeed * Time.deltaTime;
         }
 
         var cameraMoveDirection = (_movePosition - transform.position).normalized;
         var distance = Vector3.Distance(_movePosition,transform.position);
 
         _movePosition = new Vector3(
             Mathf.Clamp(_movePosition.x, BoundsMin.x, BoundsMax.x),
             Mathf.Clamp(_movePosition.y, BoundsMin.y, BoundsMax.y),
             Mathf.Clamp(_movePosition.z, BoundsMin.z, BoundsMax.z)); 
 
         if (distance > 0)
         {
             var newCameraPosition = transform.position + (cameraMoveDirection * distance * MovementSpeed  * Time.deltaTime);
 
             newCameraPosition = new Vector3(
                 Mathf.Clamp(newCameraPosition.x, BoundsMin.x, BoundsMax.x),
                 Mathf.Clamp(newCameraPosition.y, BoundsMin.y, BoundsMax.y),
                 Mathf.Clamp(newCameraPosition.z, BoundsMin.z, BoundsMax.z)); 
 
             var distanceAfterMoving = Vector3.Distance(newCameraPosition, _movePosition);
 
 
             if (distanceAfterMoving > distance)
             {
                 newCameraPosition = _movePosition;
             }
 
             transform.position = newCameraPosition;
         }
     }

so I know how to get all the values like the angle, distance, hypotenuse, and adjacent of the camera triangle, but I know I'm still missing something in the trigonometry of the camera.

My current X rotation of the camera is 40.

Just to make myself clear I want that camera movement will clamp when camera position will be for example MapXMin - cameraLeftCorner.

Can someone please help me figure out what am I doing wrong?

rtscamera.jpg (46.4 kB)
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

160 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

Related Questions

How to have free rotation camera? 1 Answer

Getting the camera to move in the direction its facing. 0 Answers

camera movments fixed.. character controller without using character controller -.-' 2 Answers

Camera to move when i move to the next room 1 Answer

Resetting Camera with a key input 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