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 Midnight6 · Dec 27, 2013 at 12:30 PM · camera

How can I do a smooth orthographic zoom with lerp?

I'm doing a 2D game and I need to change the orthographicsize and the position when the player touches a trigger. I have this script, and it works correctly but the zoom is too hard, how can I do smoothly? some idea?

using UnityEngine; using System.Collections;

public class CameraZoom : MonoBehaviour {

 public float ZoomPre;
 public float ZoomIdle;
 public float duration;
 public float ypos1;
 public float ypos2;

 
 void Start () {
     Camera.main.orthographic = true;
     
 }
 
 
 
  void OnTriggerEnter(Collider other) {
    
     if(other.name == "Player"){
         
         Camera.main.orthographicSize = Mathf.Lerp(ZoomPre, ZoomIdle, duration);
         
         Camera.main.GetComponent<CameraMovement>().ypos = Mathf.Lerp(ypos1, ypos2, duration);
         
     }
 }
 }


Thank you so much, I'm a beginner

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 Jessy · Dec 27, 2013 at 12:39 PM 0
Share

"organic" is an undefined, touchy-feely word. Please quantify.

avatar image ForbiddenSoul · Dec 27, 2013 at 12:59 PM 0
Share

Still a beginner myself, but damping (smoothing) is typically done like this:

 float yposDamping = 2.0;
 Camera.main.GetComponent<Camera$$anonymous$$ovement>().ypos = $$anonymous$$athf.Lerp(ypos1, ypos2, yposDamping * Time.deltaTime);

see if that helps...

2 Replies

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

Answer by KellyThomas · Dec 27, 2013 at 02:31 PM

Your current code seems to be adjusting the camera in a single frame (`OnTriggerEnter()`), I'm not sure what your value of duration is but you are missusing Lerp if you wish to progress from one value to another.

For your needs it is best to have the trigger set in motion a transition that occurs over multiple frames.

Please look at this example (typed without IDE, may contain typos):

 using UnityEngine;
 using System.Collections;
 
 public class CameraZoom : MonoBehaviour {
     public float Zoom1;
     public float Zoom2;
 
     public float ypos1;
     public float ypos2;
 
     public float duration = 1.0f;
     private float elapsed = 0.0f;
     private bool transition = false;
      
     void Start () {
         Camera.main.orthographic = true;
     }
 
     void Update() {
         if (transition) {
             elapsed += Time.deltaTime / duration;
             Camera.main.orthographicSize = Mathf.Lerp(Zoom1, Zoom2, elapsed);
             //this next line i'm not sure of, I'm not familiar with CameraMovement.ypos
             Camera.main.GetComponent<CameraMovement>().ypos = Mathf.Lerp(ypos1, ypos2, elapsed);
             if (elapsed > 1.0f) {
                 transition = false;
             }
         }
     }
      
     void OnTriggerEnter(Collider other) {
         if(other.name == "Player"){
            transition = true;
            elapsed = 0.0f;
         }
     }
 }


Once you are confident implementing the transition you may want to try substituting Lerp with `Mathf.SmoothStep()`. Rather than move at a linear rate, as Lerp does, SmoothStep will accelerate and decelerate at the ends. This results in a more natural looking movement. A graph can be found over here.

Also note that the trigger scripts and the camera management scripts should really be separated. The Point Of Interest should ask (not force) the camera to look at it, otherwise how will you handle the conflict when two POIs are active?

[3]: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html

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 Midnight6 · Dec 27, 2013 at 10:44 PM 0
Share

Thank you very much! you solved my problem, and thank you for helping me everybody!

avatar image
0

Answer by sparkzbarca · Dec 27, 2013 at 01:56 PM

you can use that damping also if you want it to slowly enter and slowly exit so the speed isn't uniform the whole time use slerp instead of lerp

lerp is linear interpolation each "step" goes the exact same distance

slerp is spherical each "step" is not the same.

check up on wiki if you want a better understanding or google lerp vs slerp.

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Problems Scripting a Camera 1 Answer

Toggle map/Camera [JS] 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Switching between one or more cameras in the same scene? 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