Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unable to lerp in coroutine

Discussion in 'Scripting' started by TooManySugar, Jan 25, 2016.

  1. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Hi all.

    I made an script so that menu items do appear smoothly. I'm able to make the code run within update but unable to make it run within a coroutine and I'm unable to find the issue. Anyone can see what is wrong?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MenuLerp : MonoBehaviour {
    5.  
    6.     GameObject objToMove;
    7.     float delay;
    8.  
    9.     public float lerpTime = 1f;
    10.     float currentLerpTime;
    11.  
    12.     public float moveDistance = 10f;
    13.  
    14.     Vector3 startPos;
    15.     Vector3 endPos;
    16.  
    17.     public bool inwards;
    18.     public bool outwards;
    19.  
    20.     public enum ComingFrom{up, down, right, left};
    21.     public ComingFrom direction;
    22.     /*
    23.     public bool up;
    24.     public bool down;
    25.     public bool right;
    26.     public bool left;
    27.     */
    28.  
    29.     // Use this for initialization
    30.     void Start () {
    31.    
    32.         if (inwards == true){
    33.             endPos = transform.position;
    34.  
    35.             switch (direction){
    36.                 case ComingFrom.up:
    37.                 startPos = transform.position + transform.up * moveDistance;
    38.  
    39.                 break;
    40.             case ComingFrom.down:
    41.                 startPos = transform.position + -transform.up * moveDistance;
    42.  
    43.                 break;
    44.             case ComingFrom.left:
    45.                 startPos = transform.position + transform.right * moveDistance;
    46.  
    47.                 break;
    48.             case ComingFrom.right:
    49.                 startPos = transform.position + -transform.right * moveDistance;
    50.  
    51.                 break;
    52.             }
    53.  
    54.             //StartCoroutine (test());
    55.  
    56.            
    57.             //startPos = transform.position + transform.up * moveDistance;
    58.  
    59.             //EL OUTWARDS MIENTRAS EL LERP ESTE EN UPDATE, PUES NADA.
    60.         }
    61.         if (outwards == true){
    62.             startPos = transform.position;
    63.             endPos = transform.position + transform.up * moveDistance;
    64.  
    65.         }
    66.  
    67.  
    68.     }
    69.     /*
    70.     protected void Start() {
    71.         startPos = transform.position;
    72.         endPos = transform.position + transform.up * moveDistance;
    73.     }
    74.     */
    75.  
    76.  
    77.     protected void Update() {
    78.         //reset when we press spacebar
    79.         if (Input.GetKeyDown(KeyCode.Space)) {
    80.             currentLerpTime = 0f;
    81.         }
    82.        
    83.         //increment timer once per frame
    84.         currentLerpTime += Time.deltaTime;
    85.         if (currentLerpTime > lerpTime) {
    86.             currentLerpTime = lerpTime;
    87.         }
    88.        
    89.         //lerp! // ease In
    90.         if (inwards==true){
    91.             float t = currentLerpTime / lerpTime;
    92.             t = Mathf.Sin(t * Mathf.PI * 0.5f);
    93.             transform.position = Vector3.Lerp(startPos, endPos, t);
    94.         }
    95.  
    96.         //lerp! // ease out
    97.         if (outwards==true){
    98.             float t = currentLerpTime / lerpTime;
    99.             t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
    100.             transform.position = Vector3.Lerp(startPos, endPos, t);
    101.         }
    102.     }
    103.  
    104.  
    105. /*
    106.     IEnumerator test(){
    107.  
    108.         currentLerpTime = 0f;
    109.         lerpTime = 40f;
    110.         float t = 0f;
    111.  
    112.         while ( t <lerpTime)
    113.         {
    114.             t = currentLerpTime / lerpTime;
    115.             currentLerpTime += Time.deltaTime;
    116.             //t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
    117.  
    118.             transform.position = Vector3.Lerp( new Vector3(0,500,0), new Vector3(0,0,0), t);
    119.  
    120.            
    121.             Debug.Log ("t" + t);
    122.  
    123.         }
    124.         yield return new WaitForEndOfFrame();
     
  2. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    You need to yield inside the while loop inside the test coroutine. You are basically doing the entire while loop inside the coroutine, then yielding once.
     
  3. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Running to check this! I'll tell how it goes!
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Quick addition: "yield return null;" waits for one frame.
     
  5. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Thank you Korno! and you too StarManta!!

    here I leave the script for anyone interested, great for menu items ease in / out

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MenuLerp : MonoBehaviour {
    5.  
    6.     public float delay;
    7.  
    8.     public float lerpTime = 1f;
    9.     float currentLerpTime;
    10.  
    11.     public float moveDistance = 10f;
    12.  
    13.     Vector3 startPos;
    14.     Vector3 endPos;
    15.  
    16.     public bool autostart;
    17.     public bool inwards;
    18.     public bool outwards;
    19.  
    20.     public enum ComingFrom{up, down, right, left};
    21.     public ComingFrom direction;
    22.  
    23.  
    24.     void Start () {
    25.  
    26.             endPos = transform.position;
    27.  
    28.             switch (direction){
    29.                 case ComingFrom.up:
    30.                 startPos = transform.position + transform.up * moveDistance;
    31.  
    32.                 break;
    33.             case ComingFrom.down:
    34.                 startPos = transform.position + -transform.up * moveDistance;
    35.  
    36.                 break;
    37.             case ComingFrom.left:
    38.                 startPos = transform.position + transform.right * moveDistance;
    39.  
    40.                 break;
    41.             case ComingFrom.right:
    42.                 startPos = transform.position + -transform.right * moveDistance;
    43.  
    44.                 break;
    45.             }
    46.  
    47.         if(autostart){
    48.             StartCoroutine (IEase());
    49.         }
    50.     }
    51.  
    52.  
    53.  
    54.     protected void Update() {
    55.         /*
    56.         //reset when we press spacebar
    57.         if (Input.GetKeyDown(KeyCode.Space)) {
    58.             currentLerpTime = 0f;
    59.         }
    60.      
    61.         //increment timer once per frame
    62.         currentLerpTime += Time.deltaTime;
    63.         if (currentLerpTime > lerpTime) {
    64.             currentLerpTime = lerpTime;
    65.         }
    66.      
    67.         //lerp! // ease In
    68.         if (inwards==true){
    69.             float t = currentLerpTime / lerpTime;
    70.             t = Mathf.Sin(t * Mathf.PI * 0.5f);
    71.             transform.position = Vector3.Lerp(startPos, endPos, t);
    72.         }
    73.  
    74.         //lerp! // ease out
    75.         if (outwards==true){
    76.             float t = currentLerpTime / lerpTime;
    77.             t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
    78.             transform.position = Vector3.Lerp(startPos, endPos, t);
    79.         }
    80.         */
    81.     }
    82.  
    83.     public void Ease(){
    84.         StartCoroutine (IEase());
    85.  
    86.     }
    87.  
    88.     IEnumerator IEase(){
    89.  
    90.         currentLerpTime = 0f;
    91.         lerpTime = 1f;
    92.         float t = 0f;
    93.  
    94.         yield return new WaitForSeconds(delay);
    95.  
    96.             while ( t <lerpTime)
    97.             {
    98.                 currentLerpTime += Time.deltaTime;
    99.                 if (currentLerpTime > lerpTime) {
    100.                     currentLerpTime = lerpTime;
    101.                 }
    102.  
    103.                 t = currentLerpTime / lerpTime;
    104.  
    105.                 if (outwards==true){
    106.                     t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
    107.                     transform.position = Vector3.Lerp( endPos,startPos , t);
    108.  
    109.                 }
    110.                 else{
    111.                     t = Mathf.Sin(t * Mathf.PI * 0.5f);
    112.                     transform.position = Vector3.Lerp( startPos, endPos, t);
    113.  
    114.                 }
    115.                 Debug.Log ("t" + t);
    116.                 yield return null;
     
    Last edited: Jan 25, 2016