Algorithm for setting acceleration and deceleration functions in Unity games

1. First set the structure ↓ for the relevant parameters of the transmission speed setting

public struct SpeedChangeInfo
{
    public float percent;   //改变速度的百分比
    public float dura;   //改变速度的持续时间
}

2. Speed ​​percentage, used to scale each time the speed value is obtained

public static float _percetV = 0f;
    //速度百分比
    public static float percentV
    {
        get { return 1-_percentV; }//如果速度变量就是“速度”,请修改为:“return _percentV”
        set { _percetV += value; }
    } 

3. Speed ​​value. Generally speaking, a float value is set to represent the player's moving speed, but my moveTime here represents the time to move a fixed distance, that is, the value here is inversely proportional to the speed. (If this is the movement speed, you need to modify 2, modify according to the remarks)

public static float _moveTime = 0.6f
public static float moveTime
{
    get { return percentV * _moveTime; }
    set { _moveTime = value; }
}

4. Coroutine settings

IEnumerator SpeedPercentChange(SpeedChangeInfo data)
{
    percentV = data.percent;//如果data.percent=0.6f,就是加速60%的基础移动速度。_percetV=0.6f,移动间隔就会减少40%
    yield return new WaitForSeconds(data.dura);
    percentV = -data.percent;
}

5. Call function

public void SetSpeedAndDuration(SpeedChangeInfo data)
{
    StartCoroutine(SpeedPercentChange(data));
}

Guess you like

Origin blog.csdn.net/qq_18809975/article/details/130150299