Answers for "change position with Switch"

C#
0

change position with Switch

// Use this for initialization
    void Start ()
    {
        initialPosition = transform.position;
        direction = -1;
        maxDist += transform.position.x;
        minDist -= transform.position.x;
    }
   
    // Update is called once per frame
    void Update ()
    {
        switch (direction)
        {
             case -1:
                // Moving Left
                if( transform.position.x > minDist)
                    {
                       GetComponent <Rigidbody2D>().velocity = new Vector2(-movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
                    }
                else
                    {
                       direction = 1;
                    }
                break;
             case 1:
                  //Moving Right
                if(transform.position.x < maxDist)
                    {
                        GetComponent <Rigidbody2D>().velocity = new Vector2(movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
                    }
                else
                    {
                        direction = -1;
                    }
            break;
        }
    }
Posted by: Guest on August-08-2021

C# Answers by Framework

Browse Popular Code Answers by Language