Omkar Puttagunta
Omkar Puttagunta

Reputation: 4156

Creating Simple Endless Moving Platform with cubes

Trying to create simple endless moving platform with 3 cubes of scale 70 on z(Player will not move forward, will just move left/right). The RepositionPlatform script is attached to each platform/cube which is responsible for movement and checks the z position of each platform and if it is <= -100.0f, then position is changed to (0,0,200.0f).

Problem is sometimes there is a little gap between the platforms(cubes) or there is a little overlap which I don't want.

Platforms should be placed one after each other without any gap or overlap!!!

Can anyone help find the issue looking at the script or suggest any other better way ?

The script below is attached to 3 platform game objects!!!

public class RepositionPlatform : MonoBehaviour
{
    private GameObject platformGO;

    [SerializeField]
    private float speed;

    // Start is called before the first frame update
    void Start()
    {
        platformGO = this.gameObject;
        Debug.Log("In RepositionPlatform Start method - "+ platformGO.name);
    }

    // Update is called once per frame
    void Update()
    {

        Debug.Log("In RepositionPlatform Update Method- " + platformGO.name);

        platformGO.transform.Translate(Vector3.back * Time.deltaTime * speed);

        Transform platformTransform = platformGO.transform;
        if(platformTransform.position.z <= -100.0f)
        {
            platformTransform.position = new Vector3(0,0,200.0f);
        }
    }
}

Upvotes: 0

Views: 83

Answers (1)

FacePuncher7
FacePuncher7

Reputation: 139

Probably because speed is a floating point value. You should read up on them if you haven't already.

Long story short, you aren't accounting for how far below -100 the value might have gone, you're just resetting it.

If you translate it instead, you will preserve any extra distance beyond -100 that the transform might have gone.

Try this instead:

If (transform.position.z < -100){

  transform.Translate(new Vector3(0,0,200));

}

Edit Should be Z value, not X

Upvotes: 1

Related Questions