Exemple #1
0
    // Update is called once per frame
    void Update()
    {
        //print("selected_player : "+selected_player+"id_player : "+id_player);
        if (selected_player == id_player)
        {
            if (Input.GetKey("left"))
            {
                player.transform.Translate(Vector3.left * speed * Time.deltaTime);
            }
            else if (Input.GetKey("right"))
            {
                player.transform.Translate(Vector3.right * speed * Time.deltaTime);
            }
            // application load level for restart
            if (Input.GetKeyDown("space") && b_jump == false)
            {
                player.GetComponent <Rigidbody2D>().AddForce(Vector3.up * jump, ForceMode2D.Impulse);
                b_jump = true;
            }

            // modifier personnage suivie par la camera
            if (b_camera)
            {
                cam.setOffset(this.gameObject);
                b_camera = false;
            }
        }
        else if (Input.GetKey("r"))
        {
            selected_player = 0;
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
        else if (Input.GetKey("a") || Input.GetKey("1"))
        {
            selected_player = 1;
            b_camera        = true;
        }
        else if (Input.GetKey("z") || Input.GetKey("2"))
        {
            selected_player = 2;
            b_camera        = true;
        }
        else if (Input.GetKey("e") || Input.GetKey("3"))
        {
            selected_player = 3;
            b_camera        = true;
        }
    }
    void FixedUpdate()
    {
        //print("selected_player : "+selected_player+"id_player : "+id_player);
        if (selected_player == id_player)
        {
            if (Input.GetKey("left"))
            {
                //player.GetComponent<Rigidbody2D>().AddForce(Vector3.left * speed, ForceMode2D.Force);
                player.transform.Translate(Vector3.left * speed * Time.deltaTime);
            }
            else if (Input.GetKey("right"))
            {
                //player.GetComponent<CharacterController>().Move();
                //player.GetComponent<Rigidbody2D>().MovePosition(player.GetComponent<Rigidbody2D>().position + Vector2.right * speed * Time.deltaTime);
                player.transform.Translate(Vector3.right * speed * Time.deltaTime);
            }
            // application load level for restart
            if (Input.GetKeyDown("space") && b_jump == false)
            {
                //print("jump");
                player.GetComponent <Rigidbody2D>().velocity = Vector3.zero;
                player.GetComponent <Rigidbody2D>().AddForce(Vector3.up * jump, ForceMode2D.Impulse);
                b_jump = true;
            }

            // modifier personnage suivie par la camera
            if (b_camera)
            {
                cam.setOffset(this.gameObject);
                b_camera = false;
            }
        }
        else if (Input.GetKey("r"))
        {
            selected_player = 0;
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
        else if (Input.GetKey("1") || Input.GetKey("a"))
        {
            selected_player = 0;
            b_camera        = true;
        }
        else if (Input.GetKey("2") || Input.GetKey("z"))
        {
            selected_player = 1;
            b_camera        = true;
        }
        else if (Input.GetKey("3") || Input.GetKey("e"))
        {
            selected_player = 2;
            b_camera        = true;
        }
        else if (Input.GetKey("n"))
        {
            Debug.Log("n");
            NextLevel();
        }
        if (IsVictory())
        {
            Debug.Log("Victory");
            NextLevel();
        }
    }