把移动方向设成-1,1
取startPos.x和当前位置.x差绝对值与distance比较,当大于舍得的距离时,方向取反
//-1 left 1 right -1 down 1 up
int direction = 1;
case 4://水平移动砖块
transform.Translate(new Vector2(speed * direction * Time.deltaTime, 0));
if (Mathf.Abs(startPos.x - transform.position.x) > distance) direction *= -1;
break;
case 5://垂直移动砖块
transform.Translate(new Vector2(0, speed * direction * Time.deltaTime));
if (Mathf.Abs(startPos.y - transform.position.y) > distance) direction *= -1;
break;