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 PatrickNeary · Apr 26, 2013 at 01:45 PM · arraytimelerpswitch-case

slow a lerp ? array/static problem

basically im using this to snap between 4 different zoom levels of a camera, that works fine, now i can't get my lerp to work gradually or be "smooth" any insight would be awesome,

i know im kinda of asking people to do it for me, but i just cannot figure out as this is new territory for me, thanks in advance :)

 #pragma strict
 var target : Transform ;
 var distance:float = 20.0;
 var switchDistance : int;
 var height : float = 10.0;
 var rotationDamping : float = 1.0;
 var rotateSpeed : float = 20.0;
 var speed : float = 0.001;
 private var heightArray : float[];
 private var lengthArray : float[];
 private var distanceArray : float[];
  var i :int;//indexes our array values by assigning them a number in our switch case
  
 function Start () {
 heightArray = [10.0, 20.0 ,30.0,40.0];
 lengthArray = [10.0, 20.0 ,30.0,40.0];
 distanceArray = [10.0, 100.0 ,150.0,400.0];
 switchDistance = 2.0;
  
 }
  
 function Update () {
     distance = distanceArray[i];  //here we actually set i as part of our array  
     if(Input.GetButton("rightMouse")){
         var h : float = rotateSpeed * Input.GetAxis ("Mouse X");
             transform.Rotate (0, h, 0);
     }
     switchDistance= Mathf.Clamp(switchDistance,0.0,4.0);
     distance = distanceArray[i];
     height = heightArray[i];
 }
  
 function LateUpdate() {
     var wantedRotationAngle = target.eulerAngles.y;
     var wantedHeight = target.transform.position.y + height;
     var currentRotationAngle = transform.eulerAngles.y;
     var currentHeight = transform.position.y;
    
        if (Input.GetAxis("mouseZoom")>0 && switchDistance <=4 ){
            Debug.Log("+");
            switchDistance -= 1 ;
        
        }  
        if (Input.GetAxis("mouseZoom")<0 && switchDistance >=0){
            switchDistance += 1 ;
        }
    
         switch (switchDistance){
        
         case 3:
             //distance = 75.0;height = 15; break;
              
               //transform.position.x=Mathf.Lerp(heightArray[3],heightArray[3],Time.time);Debug.Log("case3");break;  
               i= 3;transform.position.x=Mathf.Lerp(distanceArray[3],distanceArray[3],Time.time*speed);Debug.Log("case3Lngth");break;
         case 2:
              
               //transform.position.x=Mathf.Lerp(heightArray[2],heightArray[3],Time.time);Debug.Log("case2");break;  
               i=2;transform.position.x=Mathf.Lerp(distanceArray[2],distanceArray[3],Time.time*speed);Debug.Log("case2Lngth");break;  
        
         case 1:
              
               //transform.position.x=Mathf.Lerp(heightArray[1],heightArray[2],Time.time);Debug.Log("case1");break;  
               i=1;transform.position.x=Mathf.Lerp(distanceArray[1],distanceArray[2],Time.time*speed);Debug.Log("case1Lngth");break;
        
         case 0:                                                                                        
              
               //transform.position.x=Mathf.Lerp(distanceArray[0],distanceArray[1],Time.time);Debug.Log("case0");break;  
               //transform.position.z=Mathf.Lerp(lengthArray[0],lengthArray[1],Time.time);Debug.Log("case0Lngth");break;
               i= 0;transform.position.x=Mathf.Lerp(heightArray[0],heightArray[1],Time.time*speed);Debug.Log("case0Lngth");break;
         }
    
    
    
    
     currentRotationAngle = Mathf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime);
     var currentRotation = Quaternion.Euler(0,currentRotationAngle,0);
     transform.position = target.position;
     transform.position -= currentRotation*Vector3.forward*distance;
     transform.position.y = wantedHeight;
     transform.LookAt (target);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Apr 26, 2013 at 02:08 PM

There are two ways you can approach this problem. The first is a "misuse" of Lerp() that is commonly used and results in an easing of the movmenet:

 transform.position.x=Mathf.Lerp(transform.position.x,distanceArray[3],Time.deltaTime*speed);

The second is more traditional. It involves you building a timer. The timer gets reset to 0 at the beginning of the movement and then is incremented at every Update() call:

 timer += Time.deltaTime;
 transform.position.x=Mathf.Lerp(distanceArray[2],distanceArray[3],timer*speed);

Note that the last parameter in the Lerp() call is a fraction between 0 and 1. When the value is 0, it will be at the first position. When the value is 1 it will be at the second position. Except for a brief time at the beginning of the game, Time.time will always be greater than 1.0.

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 PatrickNeary · Apr 26, 2013 at 02:59 PM 0
Share

is timer a float?

avatar image robertbu · Apr 26, 2013 at 03:09 PM 0
Share

Yes, timer is a float:

 var timer : float = 0.0;
avatar image PatrickNeary · Apr 26, 2013 at 03:17 PM 0
Share

i almost have it working, ended up with something like this, lerp is now at the bottom, cases govern array value only #pragma strict var target : Transform ; var distance:float = 20.0; var switchDistance : int; var height : float = 10.0; var rotationDamping : float = 1.0; var rotateSpeed : float = 20.0; var speed : float = 2.0; private var heightArray : float[]; private var lengthArray : float[]; private var distanceArray : float[]; var i :int;//indexes our array values by assigning them a number in our switch case var timer : float; function Start () { heightArray = [10.0, 20.0 ,30.0,40.0]; lengthArray = [10.0, 20.0 ,30.0,40.0]; distanceArray = [10.0, 100.0 ,150.0,400.0]; switchDistance = 2.0; } function Update () { distance = distanceArray[i]; //here we actually set i as part of our arr timer += Time.deltaTime; if(Input.GetButton("right$$anonymous$$ouse")){ var h : float = rotateSpeed * Input.GetAxis ("$$anonymous$$ouse X"); transform.Rotate (0, h, 0); } switchDistance= $$anonymous$$athf.Clamp(switchDistance,0.0,4.0); distance = distanceArray[i]; height = heightArray[i]; } function LateUpdate() { var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.transform.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; if (Input.GetAxis("mouseZoom")>0 && switchDistance <=4 ){ Debug.Log("+"); switchDistance -= 1 ; } if (Input.GetAxis("mouseZoom")<0 && switchDistance >=0){ switchDistance += 1 ; } switch (switchDistance){ case 3: //distance = 75.0;height = 15; break; //transform.position.x=$$anonymous$$athf.Lerp(heightArray[3],heightArray[3],Time.time);Debug.Log("case3");break; i= 0;break; case 2: //transform.position.x=$$anonymous$$athf.Lerp(heightArray[2],heightArray[3],Time.time);Debug.Log("case2");break; i=1;break; case 1: //transform.position.x=$$anonymous$$athf.Lerp(heightArray[1],heightArray[2],Time.time);Debug.Log("case1");break; i=2;break; case 0: //transform.position.x=$$anonymous$$athf.Lerp(distanceArray[0],distanceArray[1],Time.time);Debug.Log("case0");break; //transform.position.z=$$anonymous$$athf.Lerp(lengthArray[0],lengthArray[1],Time.time);Debug.Log("case0Lngth");break; i= 3; break; } currentRotationAngle = $$anonymous$$athf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime); var currentRotation = Quaternion.Euler(0,currentRotationAngle,0); transform.position = target.position; transform.position -= currentRotation*Vector3.forward*distance; transform.position.y = wantedHeight; transform.position.x =$$anonymous$$athf.Lerp(transform.position.x,distanceArray[i],Time.deltaTime*speed); transform.LookAt (target); }

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

12 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

Related Questions

Smooth gradient between colours? 1 Answer

How to move along a group of waypoints at a certain speed? 2 Answers

c# wait script 2 Answers

Trying to lerp in different states in a switch statement 0 Answers

Mathf.Lerp Question 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