首页 > 代码库 > 根据距离加速

根据距离加速

using UnityEngine;
using System.Collections;

public class addSpeed : MonoBehaviour {

    public float scrollSpeed;

    float defaultScroll = 0.01f;
    float maxScrollSpeed = 0.7f;
    public float maxScrollSpeedDist = 100;
    public float distance = 0;

    // Use this for initialization
    void Start () {
        scrollSpeed = defaultScroll;
    }
    
    // Update is called once per frame
    void Update () {

        if(distance < maxScrollSpeedDist){
            //根据距离计算增速
            if(maxScrollSpeed > scrollSpeed)
                scrollSpeed = defaultScroll + (((maxScrollSpeedDist - (maxScrollSpeedDist - distance)) / maxScrollSpeedDist) * (maxScrollSpeed - defaultScroll));
            //根据速度做移动
            transform.position -= Vector3.right * scrollSpeed * Time.deltaTime * 200;
            //计算距离
            distance += scrollSpeed * Time.deltaTime * 250;
        }
    }
}