Vector3 acc = Vector3.zero;
Vector3 diff;
if (Input.GetKey(KeyCode.LeftArrow))
{
acc.x = -0.1f;
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.localScale = new Vector3(1, 1, 1);
acc.x = 0.1f;
}
diff = Vector3.MoveTowards(transform.localPosition, transform.localPosition + acc, 0.5f * Time.time);
diff.y = transform.localPosition.y;
diff.z = 0;
//设置边界
if (diff.x < leftBorder)
{
diff.x = rightBorder;
}
if (diff.x > rightBorder)
{
diff.x = leftBorder;
}
transform.localPosition = diff;