永远在 x 方向上移动和传送一个对象

Moving and teleporting an object in the x direction forever

所以我遇到了这个让我沮丧了一段时间的问题,我需要让 3 个游戏对象以基本设定速度的 %speed 移动。那部分很简单。现在的问题是我无法让它移动到设定位置,然后传送到我面前并继续, 非常感谢您的帮助!

What I'm going for

Current code

更新函数

    void Update()
{
    //suuuuper bad implemantation
    float newPos = Mathf.Repeat(Time.time * globalspeed, 20);
    Ground1.transform.position = startpos + new Vector2(reset, upPos1) * newPos * Ground1Speed;
    Ground2.transform.position = startpos + new Vector2(reset, upPos2) * newPos  * Ground2Speed;
    Ground3.transform.position = startpos + new Vector2(reset, upPos3) * newPos * Ground3Speed;

}

我不确定我明白你想要什么,但如果我是对的,你想要 3 个游戏对象在地面上移动(就像一辆正在行驶的汽车)并在它们到达尽头时消失,然后出现在开始位置并再次无限循环移动。如果我是对的,你应该试试这个:

public float objectSpeed;

void update()
{
    MoveObject();
}

void MoveObject()
{

    objectSpeed = 20; //Set the object speed
    
    if (transform.position.x >= 120) //Set the position to where you want your object to disappear
    {
        disapper(gameObject);
    }

    if (transform.position.x >= 300) //This position is related to when the object will appear again in the start position
    {
        appear(gameObject);
    }
    
    void disappear()
    {
        transform.Find(gameObject).GetComponent<MeshRenderer>().enabled = false;
    }

    void appear()
    {
        transform.Find(gameObject).GetComponent<MeshRenderer>().enabled = true;
    }
} // Drag this script to 3 of the game object you want them to move. This code is no tested but should work fine.