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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Mathf.Lerp(a, b, t) not completing within t

Discussion in 'Scripting' started by ElnuDev, Aug 8, 2018.

  1. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    I have a circle which every 0.125 seconds rotates in a random direction then Lerps to the location in 0.125 seconds. However, it cannot complete the action in time and is much to slow. It takes at least 0.25 seconds to complete the task.

    Does anyone have any ideas? Here is my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PivotPoint : MonoBehaviour {
    6.  
    7.     public float pauseTime;
    8.     public GameObject eye;
    9.     public GameObject bulletPrefab;
    10.     public float bulletSpeed;
    11.     private float targetRotation;
    12.     public bool instantRotation;
    13.  
    14.     private void Start()
    15.     {
    16.         StartCoroutine("Rotate");
    17.     }
    18.  
    19.     IEnumerator Rotate ()
    20.     {
    21.         targetRotation = Random.Range(0, 360);
    22.         if (instantRotation)
    23.         {
    24.             transform.parent.eulerAngles = new Vector3(transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, Random.Range(0, 360));
    25.         } else
    26.         {
    27.             float distance = Mathf.Abs(transform.parent.eulerAngles.z - targetRotation);
    28.             float targetTime = Time.time + pauseTime;
    29.             while (Mathf.Abs(transform.parent.eulerAngles.z - targetRotation) > 1 && Time.time <= targetTime) { // Keep rotating until you reach the target, or until time runs out
    30.                 transform.parent.eulerAngles = Vector3.forward * Mathf.Lerp(transform.parent.eulerAngles.z, targetRotation, pauseTime); // Rotate
    31.                 eye.transform.position = transform.position; // Move eye to location rotation
    32.                 yield return new WaitForEndOfFrame(); // Wait until end of frame
    33.             }
    34.             transform.parent.eulerAngles = new Vector3(transform.parent.eulerAngles.x, transform.parent.eulerAngles.y, Random.Range(0, 360)); // Snap to final destination (this is always jittery, because it cannot reach the final destination)
    35.         }
    36.         GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.position, Quaternion.identity, null); // Spawn bullet
    37.         bullet.GetComponent<Rigidbody2D>().velocity = transform.up * bulletSpeed; // Give bullet velocity
    38.         if (instantRotation)
    39.         {
    40.             eye.transform.position = transform.position;
    41.             yield return new WaitForSeconds(pauseTime);
    42.         }
    43.         StartCoroutine("Rotate"); // Rotate again
    44.     }
    45. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your lerp is all kinds of wrong. You're moving the A parameter and T is a static value rather than growing from 0 to 1.

    This is how to do it properly:
    Code (csharp):
    1.  
    2. float duration = 0.125f;
    3. float time = 0f;
    4.  
    5. float startValue = // whatever
    6. float endValue = // whatever
    7.  
    8. while(time <= duration)
    9. {
    10.    time += Time.deltaTime;
    11.  
    12.    float value = Mathf.Lerp(startValue, endValue, time / duration);
    13.  
    14.    // assign the value to whatever
    15.  
    16.    yield return null;
    17. }
    18.  
    Further reading here: https://chicounity3d.wordpress.com/2014/05/23/how-to-lerp-like-a-pro/
     
    ElnuDev likes this.
  3. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Thanks so much! It worked like a charm.