我放置了一个平台,它应该从放置它的位置左右移动,但是问题是它移动到最右端然后又来回移动。我正在使用Unity和C#
//Left to Right moving platform
public float dirX, moveSpeed;
private bool moveRight = true;
private void Update()
{
if (transform.position.x > 4f)
moveRight = false;
if (transform.position.x < -4f)
moveRight = true;
if (moveRight)
transform.position = new Vector2(transform.position.x + moveSpeed * Time.deltaTime, transform.position.y);
else transform.position = new Vector2(transform.position.x - moveSpeed * Time.deltaTime, transform.position.y);
}
答案 0 :(得分:0)
您最初的变换位置是什么?它是原点(0,0,0)吗?如果是这样,请更改代码以进行检查:
if (transform.position.x < 0f) // this should be your platforms initial transform.position.x value
moveRight = true;