我怎样才能在一致的情况下无限地上下移动物体

时间:2016-12-10 08:03:42

标签: unity5

我想在unity5(2d游戏)中无限地上下移动物体 起始位置是当前位置,目标位置是当前位置+ y矢量(0,y,0) 我希望控制对象的速度。

1 个答案:

答案 0 :(得分:1)

public class moleMove:MonoBehaviour {

Vector3 current_position;
float direction = 1.0f;
float speed = 1.5f;
float heightlimit = 0.8f;
float timecount = 0.0f;
float timelimit = 2.5f;

void Start(){
    current_position = this.transform.position;
}

void Update() {

    transform.Translate (0, direction*speed*Time.deltaTime * 1, 0);


    if (transform.position.y >current_position.y+heightlimit) {
        direction = -1;
    }
    if (transform.position.y <current_position.y){
        direction = 0;
        timecount = timecount + Time.deltaTime;

        if (timecount > timelimit) {
            direction = 1;
            timecount = 0;
        }
    }
 }

}