• 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 Flazone · Feb 27, 2015 at 04:12 PM · c#zoom

Zooming with a gun problem (C#)

Hi all,

I'm trying to do a zomming features on my game and I got a problem ...

Got this in my MouseLook C# script attached to my Camera :

     //zooming variables
     public float defaultCameraAngle = 60.0f;
     public float currentTargetCameraAngle = 60.0f;
     public float ratioZoom = 1.0f;
     public float ratioZoomV; 
     public float ratioZoomSpeed = 0.2f; 


     //some stuff


         if (Input.GetKey(playerInputsComponent.aiming) == false) { //right click
             ratioZoom = Mathf.SmoothDamp (ratioZoom, 1, ref ratioZoomV, ratioZoomSpeed);
         }
         else {
             ratioZoom = Mathf.SmoothDamp (ratioZoom, 0, ref ratioZoomV, ratioZoomSpeed);
         }
     
         camera.fieldOfView = Mathf.Lerp(currentTargetCameraAngle, defaultCameraAngle, ratioZoom);


And got my weaponScript attached to my weapon :

     //zoom
     public float zoomAngle = 30;

     //lot of stuff

 cameraObject.GetComponent<SmoothMouseLook>().currentTargetCameraAngle = zoomAngle;


So when I pressed the right click it's supposed to zoom smoothly to 30 degress FOV but it zoom without stopping down to 0 and it varies between 0 and 10...

Can someone help me please ? I don't understand what happens..

Hope you understand the problem, thanks in advance!

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 meat5000 ♦ · Feb 27, 2015 at 04:30 PM 0
Share

You are using Lerp incorrectly if you intend for it to create a smooth change.

avatar image Flazone · Feb 27, 2015 at 04:33 PM 0
Share

Thanks for your answer, yes I'm using Lerp to create a smooth zoom but how I can resolve that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by meat5000 · Feb 27, 2015 at 04:35 PM

Lerp returns 1 single interpolated value each time you call it.

To create a smooth motion you vary t from 0 to 1 and call Lerp repeatedly. It's best performed in Update.

Comment
Add comment · Show 3 · 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 Flazone · Feb 28, 2015 at 10:31 AM 0
Share

Thanks for tou answer but sorry I dont get it, when I do that :

         camera.fieldOfView = ($$anonymous$$athf.Lerp(currentTargetCameraAngle, defaultCameraAngle, Time.time);

It automatically zoom out when I launch the game

avatar image meat5000 ♦ · Feb 28, 2015 at 04:44 PM 0
Share

Check the docs.

t factor only works between 0 and 1. After you launch the game or create the object or whatever, Time has already passed 1 second so you only ever get the 'defaultCameraAngle'.

avatar image Flazone · Feb 28, 2015 at 08:08 PM 0
Share

Did I need to use a Vector3.Lerp ins$$anonymous$$d of $$anonymous$$athf.Lerp ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Show far away objects as a circle (big dot) 2 Answers

Flip over an object (smooth transition) 3 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