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;
}
}
}