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
1
Question by uberokeer · Jul 06, 2013 at 06:39 PM · camerartsclamp

Clamp Mouse Zoom using Scroll Wheel (SOLVED)

Hello, I'm making a RTS game and i created a little line of code to control mouse zoom here's the code.

 var ROTSpeed = 10f;
 function Update()
 {
 gameObject.transform.Translate(0,0,Input.GetAxis("Mouse ScrollWheel") * ROTSpeed);
 }

So that's My code, it works just fine. Perfectly in fact, but I have one problem i can zoom in and out infinitely. Does anyone know how i could clamp the zoom? Thanks in advance! :D

Comment
Add comment · Show 4
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 clunk47 · Jul 06, 2013 at 07:16 PM 1
Share

Just use a float for max and $$anonymous$$ zoom values.

 var ROTSpeed = 10f;
 var $$anonymous$$ : float = -10.0;
 var max : float = 10.0;
 
 function Start()
 {
     $$anonymous$$ = Camera.main.fov + $$anonymous$$;
     max = Camera.main.fov + max;
 }
 
 function Update()
 {
     if(Camera.main.fov <= $$anonymous$$)
         Camera.main.fov = $$anonymous$$;
     if(Camera.main.fov >= max)
         Camera.main.fov = max;
     Camera.main.fov += Input.GetAxis("$$anonymous$$ouse ScrollWheel") * ROTSpeed;
 }
avatar image uberokeer clunk47 · Jul 06, 2013 at 07:37 PM 0
Share

:/ this isn't working for me. What is Camera suppose to be. I don't see it declared anywhere... And this script is supposed to be on the main camera. I tried making a var called thisCam and then replaced all the Camera parts with thisCam and then i set thisCam to be the main camera. And I'm getting a error. it just says,

NullReferenceException UnityEngine.Camera.get_fov ()

avatar image clunk47 clunk47 · Jul 06, 2013 at 08:56 PM 0
Share

Camera.main is the main camera. All you have to do is make sure the camera in your scene is tagged "$$anonymous$$ain Camera" in the inspector. Once you do this the script will no longer return null.

avatar image clunk47 clunk47 · Jul 06, 2013 at 08:57 PM 0
Share

You could just add

var cam : Camera;

at the top of your script, then drag which camera you want to use. then you'd just use cam.fov.

2 Replies

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

Answer by Filippo94 · Jul 06, 2013 at 07:26 PM

Store the amount in a variable, then clamp it. For example (UnityScript):

 var ZoomAmount : float = 0; //With Positive and negative values
 var MaxToClamp : float = 10;
 var ROTSpeed : float = 10;
 
 function Update() {
     ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
     ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
     var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp - Mathf.Abs(ZoomAmount));
     gameObject.transform.Translate(0,0,translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
 }

Untested.

Comment
Add comment · Show 7 · 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 uberokeer · Jul 06, 2013 at 07:49 PM 0
Share

Thanks Worked Perfectly! :D

avatar image Filippo94 · Jul 06, 2013 at 08:08 PM 1
Share

You're Welcome! :)

avatar image clunk47 · Jul 06, 2013 at 08:58 PM 0
Share

$$anonymous$$uch better answer than $$anonymous$$e +1 :)

avatar image CiaranLyne · Feb 05, 2015 at 07:41 AM 0
Share

Another +1 from me.

avatar image JefferyWright · Jan 08, 2016 at 08:48 PM 1
Share

This produced errors for me:

 Assets/$$anonymous$$WZoom2.cs(6,24): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
 
 Assets/$$anonymous$$WZoom2.cs(6,32): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
 
 Assets/$$anonymous$$WZoom2.cs(7,24): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
 
 Assets/$$anonymous$$WZoom2.cs(7,32): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
 
 Assets/$$anonymous$$WZoom2.cs(8,22): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
 
 Assets/$$anonymous$$WZoom2.cs(8,30): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
 

Script file:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$WZoom2 : $$anonymous$$onoBehaviour {
 
     var ZoomAmount : float = 0; //With Positive and negative values
     var $$anonymous$$axToClamp : float = 10;
     var ROTSpeed : float = 10;
     
     function Update() {
         ZoomAmount += Input.GetAxis("$$anonymous$$ouse ScrollWheel");
         ZoomAmount = $$anonymous$$athf.Clamp(ZoomAmount, -$$anonymous$$axToClamp, $$anonymous$$axToClamp);
         var translate = $$anonymous$$athf.$$anonymous$$in($$anonymous$$athf.Abs(Input.GetAxis("$$anonymous$$ouse ScrollWheel")), $$anonymous$$axToClamp - $$anonymous$$athf.Abs(ZoomAmount));
         gameObject.transform.Translate(0,0,translate * ROTSpeed * $$anonymous$$athf.Sign(Input.GetAxis("$$anonymous$$ouse ScrollWheel")));
     }
 
 }
Show more comments
avatar image
2

Answer by newking777 · Jan 06, 2017 at 04:59 PM

For those that still need an answer to this, using Mathf Clamp is not the best way. Instead, simply write the following code on Update()

     if (Input.GetAxis("Mouse ScrollWheel") > 0)
     {
         if (mycamera.fieldOfView > 1)
         {
             mycamera.fieldOfView--;
         }
     }
     if (Input.GetAxis("Mouse ScrollWheel") < 0)
     {
         if (mycamera.fieldOfView < 100)
         {
             mycamera.fieldOfView++;
         }
     }


This only zooms in if the field of view is greater than the minimum (which is 1 for all cameras). The maximum value is greater than 100 but 100 is a good value.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

simple clamp camera translate on Y axis only 2 Answers

I can't figure out how to fix an "Expressions in statements must only be executed for their side-effects." problem. 1 Answer

BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers

Character camera always moving to a relative height 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