Answers for "unity on switch case change"

C#
1

unity switch

public float number;

switch(number)
{
case 1: 
Debug.Log("1");
break;
case 2:
Debug.Log("1");
break;
default: //case that happens if no case matches
Debug.Log("I dont know");
break;
}
Posted by: Guest on February-03-2021
0

change position with Switch unity

// 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