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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to Start with a hight speed value and End with a speed null in a predetermined mouvement

Discussion in 'Scripting' started by Y0ko, Jul 5, 2019.

  1. Y0ko

    Y0ko

    Joined:
    Jun 25, 2014
    Posts:
    17
    Hello
    I use a custom character controller and I have created a simple dash Up movement on my character with a predetermined distance to reach.

    Code (CSharp):
    1. void DashUp()
    2.     {
    3.        
    4.        
    5.         velocity = new Vector2(0, 1 * rocketSpeedUsed);
    6.         velocity.x = directionalInput.x * (moveSpeed);
    7.  
    8.      
    9.         if (elapsedTime >= RocketDuration)
    10.         {
    11.            StartCoroutine(CoolDownRocketUp(rocketCD));
    12.            
    13.             etat = PlayerState.basic; // change of player's state
    14.            
    15.             isRocket = false;
    16.          
    17.             velocity.y = 0f;
    18.          
    19.            
    20.         }
    21.  
    22.         elapsedTime += Time.deltaTime;
    23.         controller.Move(velocity * Time.deltaTime);
    24.     }

    And to be sure to reach the good distance in the good timeframe i use the equation "distance / time" to get the speed.

    Code (CSharp):
    1. float GetSpeedDash(Vector2 DashPos, float DashDuration)
    2.     {
    3.  
    4.         float dist = Vector2.Distance(transform.position, DashPos);
    5.         float t = DashDuration;
    6.         float speedD = dist / t;
    7.        
    8.  
    9.         return speedD;
    10.     }
    And now, I'm trying to make my dash start at a higher speed and slow down to 0 when it reaches the intended destination (keep the same dash distance). But each try was a failure :(.

    If you have an idea, I would appreciate it.:)
     
  2. Thibault-Potier

    Thibault-Potier

    Joined:
    Apr 10, 2015
    Posts:
    206
  3. Y0ko

    Y0ko

    Joined:
    Jun 25, 2014
    Posts:
    17
    I have already try to use a Lerp() with something like that:

    Code (CSharp):
    1. public IEnumerator SpeedVariation()
    2.     {
    3.         float t = 0f;
    4.    
    5.         float speedRocket = rocketSpeed * SpeedRocketStart;
    6.  
    7.         float n = RocketDuration / SpeedRocketStart;
    8.    
    9.      
    10.         while (t < RocketDuration)
    11.         {
    12.                 //rocketSpeedUsed = Mathf.Lerp(speedRocket, 0, (t / RocketDuration))
    13.                 rocketSpeedUsed = Mathf.Lerp(speedRocket, 0, (t / n));
    14.  
    15.             t += Time.deltaTime;
    16.             yield return null;
    17.   }
    The idea is to keep the same average speed as the dash without the speed variation.

    For example if the distance covered by the basic dash is 10 meters in 0.5 seconds, the average speed of the dash will be 20 meters per second.

    Now we want to keep the same average speed (20 m/s), but with a starting speed of for example 40 m / s. This speed must be reduced during the journey so that in the end, when the character has reached the end of the dash (10 meters), the average speed of the journey remains 20 m/s.

    And i have no good idea how i can do this :(
     
    Last edited: Jul 6, 2019