D
D
Dark80902020-07-24 14:32:05
Unity
Dark8090, 2020-07-24 14:32:05

[Unity2D] How to set up the platform?

There is a platform script, how to make it possible to assign in the inspector where it will move? We need X and Y axes.

public class MoveingPlatform : MonoBehaviour
{
    float speed = 3f;
    bool moveingRight = true;


    [SerializeField] public float x;

    void Update()
    {
        
        if (transform.position.x > x)
        {
            moveingRight = false;
        }
        else if (transform.position.x < -x)
        {
            moveingRight = true;
        }
        if (moveingRight)
        {
           transform.position = new Vector2(transform.position.x + speed * Time.deltaTime, transform.position.y);
        }
        else
        {
            transform.position = new Vector2(transform.position.x - speed* Time.deltaTime, transform.position.y);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question