0

It should smooth slowly increase the speed from 0 to 1 then slowly smoothly decrease from 1 to 0 then again from 0 to 1 and so on nonstop.

but what it does now it's just increasing the speed value all the time. and maybe there is a simple way shorter code to do it?

public Animator playerAnimator;
public float speed;

private float t = 0.0f;
private float min = 0;
private float max = 1;
private bool maxed = false;

void Update()
    {
        if (t == 0)
        {
            maxed = true;
        }
        
        if(t == 1)
        {
            maxed = false;
        }

        if(maxed)
        {
            t += speed * Time.deltaTime;
            playerAnimator.SetFloat("Forward", Mathf.Lerp(min, max, t));
        }
        else
        {
            t -= speed * Time.deltaTime;
            playerAnimator.SetFloat("Forward", Mathf.Lerp(min, max, t));
        }
    } 
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 2
    with floats, it's better to use `<=` and `>=` for comparisons (like `if(t >= max) { t = max; maxed = false; }` ), since the value may never *exactly* equal `1`. See the answers to [this question](https://stackoverflow.com/questions/3874627/floating-point-comparison-functions-for-c-sharp) for more info. Also, since you created `min` and `max`, you may as well use them instead of hard-coded values. – Rufus L Jun 10 '21 at 18:36

1 Answers1

1

You can use Mathf.PingPong to go back and forth between 2 values.

public Animator playerAnimator;
public float speed;

private float t = 0.0f;
private float min = 0;
private float max = 1;

void Update()
{
    t += speed * Time.deltaTime;
    playerAnimator.SetFloat("Forward", Mathf.PingPong(t, max-min) + min);
}

Since Mathf.PingPong is only able to PingPong between 0 and the length that you give it and not between a minimum and maximum value you need to give it the length of max-min (essentially the length between the max and the min) and then add min back to the result so that it starts at min and not at 0.

joll05
  • 420
  • 5
  • 9
  • if I want to make it with time? I mean that it will do the same as now but for example, I can set the time for increasing the speed to take 5 seconds and to slow down 2 seconds so the increasing and decreasing will not be the same time. it will move between 1 and 0 like now but with time. For example from 0 to 1 it will take 5 seconds and from 1 to 0 it will take 2 second. – Daniel Lip Jun 10 '21 at 18:55
  • and another question what should I change if I want it to slow down close to 0 but not to stop at 0 just get very close to 0 and then increasing? for example, instead getting to 0 it will get to 0.1 – Daniel Lip Jun 10 '21 at 19:01
  • 2
    @DanielLip I'm not sure if there is an easy solution to have different speeds for going up and going down using Mathf.PingPong. I would imagine a solution would change the speed which `t` changes at depending if it's going between an even number and an odd number or the other way around, but I don't know how to do that. To make it just get close to 0 you could just change `min` to be 0.1 (or some other value). – joll05 Jun 10 '21 at 19:05
  • 1
    @DanielLip as joll05 mentioned in the comment before in such a case rather use what you had before with two different speed values ... your actual issue in your code is rather floating point imprecision ... **NEVER** use `==` for comparing floats ... you would rather have wanted to use `if(t <= 0){ ... } else if (t >= 0){ ... }` – derHugo Jun 11 '21 at 12:12