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 Xicer · Jul 29, 2012 at 10:38 PM · camerazoom

Need to limit zoom in/out of camera script

Hello

i have this very simple camera scripe

 var cam : Camera;
 
 function Update(){
     cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");
 }

I need to limit the max zoom in and out of the camera.

If someone can point me in the right direction, on how i would go about doing this.

Thanks in advance

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

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by LukaKotar · Feb 02, 2013 at 02:05 PM

 var cam : Camera;
 var min : float; //Min FOV (zoomed in)
 var max : float; //Max FOV (zoomed out)
  
 function Update(){
     cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");

     //Use Mathf.Clamp to keep the value always lower than 'max' and greater than 'min'
     cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, min, max);
 }
Comment
Add comment · Show 1 · 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
1

Answer by Ut1lityDev · Dec 13, 2020 at 09:33 AM

Here is the C# version of @LukaKotar's script.

 public Camera camera;
 float min;
 float max;
 
 void Update() {
     cam.fieldOfView += 5 * Input.GetAxis("Mouse ScrollWheel");
     cam.fieldOfView = Mathf.Clamp(cam.fieldOfView, min, max);
 }
Comment
Add comment · 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
0

Answer by joeyaaaaa · Jul 29, 2012 at 11:24 PM

var distanceMin = 4; var distanceMax = 45;

function LateUpdate () { if (target) { distance = Mathf.Clamp(distance - Input.GetAxis("Camera Zoom")5, distanceMin, distanceMax); if (Input.mousePosition.x > Screen.width - 2 || Input.mousePosition.x < 2 || Input.mousePosition.y > Screen.height - 2 || Input.mousePosition.y < 2) DoIT = 1; else DoIT = 0; if (FirstLogin == 0) { FirstLogin = 1; DoIT = 1; } if (DoIT == 1) { if (Input.mousePosition.x > Screen.width - 2) x += 60 xSpeed 0.02 Time.deltaTime; if (Input.mousePosition.x < 2) x -= 60 xSpeed 0.02 Time.deltaTime; if (Input.mousePosition.y < 2) y += 30 ySpeed 0.02 Time.deltaTime; if (Input.mousePosition.y > Screen.height - 2) y -= 30 ySpeed 0.02 * Time.deltaTime; y = ClampAngle(y, yMinLimit, yMaxLimit);

var rotation = Quaternion.Euler(y, x, 0); var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; var hit : RaycastHit; if (Physics.Linecast (target.position, transform.position, hit)) { distance -= hit.distance; }

     transform.rotation = rotation;
     transform.position = position;
     lastPos = transform.position - target.position;
     }
     else transform.position = target.transform.position + lastPos;

} }

static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }

function StartTheCam() { distance = Mathf.Clamp(distance - Input.GetAxis("Camera Zoom")5, distanceMin, distanceMax); var angles = transform.eulerAngles; x = angles.y; y = angles.x; y = ClampAngle(y, yMinLimit, yMaxLimit); var rotation = Quaternion.Euler(y, 0, 0); var position = rotation Vector3(0.0, 0.0, -distance) + target.position;

transform.rotation = rotation; transform.position = position; lastPos = transform.position - target.position; transform.position = target.transform.position + lastPos; }

i dont really know how to do it but this works but the zoom doesnt show it has worked till you move the camera again. it mightjust be in the wrong spot lol

Comment
Add comment · 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
0
Wiki

Answer by Ingen · Jul 30, 2012 at 01:20 AM

maybe something like this?

pseudo code not expect can work at now, just to have a line of think. Hope can help

 var cam : Camera;
 var  Dist : Float;
 var minDist : Float;
 var maxDist : Float;
 var zoom : boolean;
 
  function update
 
      if Dist<=minDist = false
          if Dist>=maxDist = false
 
 if {
       input scrollMouseDown
        Zoomin;
     }
 
 if {
    input scrollMouseUP
        ZoomOut;
    }
 function ZoomIn (){
   cam.fieldOfView += 5
 }
 
 function ZoomOut (){
   cam.fieldOfView -= 5
 }


good look

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Scoping script /Camera zoom preferably C# 3 Answers

Camera Zoom Input change. 1 Answer

How to move camera on local z axis 2 Answers

Changing camera position to shoot through scope on gun 1 Answer

Raycast misses after camera zoom 2 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