Creating a floating platform, moving up and down gently

Hello everyone,

I’ve got the following script I’m using to create a floating platform, but it seems a bit wonky, not exactly smooth, and I was just wondering if there’s a better way or a recommended way to do something like this. Basically I have little creatures standing on platforms and I have this script to make the platform move up and down continuously. But I’m hard coding coordinates for the Y coordinate, so that means I’d have to hard code it differently for each platform, which I’m sure isn’t optimal.

Any other suggestions than what I’m doing here? Or is this pretty much what you would do?

public class PlatformMovement : MonoBehaviour {

    private float originalYPosition;
    private float originalXPosition;
    private bool movedUp = false;
    private bool movedDown = false;
    private float moveBy = .4f;

    // Use this for initialization
    void Start () {
        originalYPosition = transform.position.y;
        originalXPosition = transform.position.x;

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

        if(!movedUp)
        {
            Vector2 position = Vector2.Lerp((Vector2)(transform.position), new Vector2(originalXPosition, 2.6f), Time.fixedDeltaTime);
            transform.position = position;
        }
        else if(!movedDown)
        {
            Vector2 position = Vector2.Lerp((Vector2)(transform.position), new Vector2(originalXPosition, 2.0f), Time.fixedDeltaTime);
            transform.position = position;
        }

        if(transform.position.y >= 2.59f)
        {
            movedUp = true;
            movedDown = false;
        }
        else if(transform.position.y <= 2.01f)
        {
            movedDown = true;
            movedUp = false;
        }

    }
}

For the movement, it’s probably better to use MovePosition, since it interacts better with physics.

For you problem with hard coded positions, I usually setup my movable object as a child of an empty gameobject and do all movement relative to the parent. And of course you could have the coordinates as a public variable and set them in the inspector for each gameobject, so that they aren’t hard coded in the script.

1 Like

Thanks so much PGJ! I thought about using an empty gameobject for each platform but didn’t think of using one for all of them. That makes much more sense, and should be easier to code. I’ll look into using the MovePosition too.

Cheers,

David

I forgot to add; using relative positioning makes it easy to create prefabs from them as well.

Hey, can you explain to me what are you doing at this point?

Vector2 position = Vector2.Lerp((Vector2)(transform.position), new Vector2(originalXPosition, 2.6f), Time.fixedDeltaTime);

I got everything till you used Lerp, but after how does the part (Vector2)(transform.position) works?

Sorry to bother, I’m just getting started and still getting the hang of things.

This is a great tutorial i used to make my moving platforms. Very easy to understand and manipulate.