コード例 #1
0
    // Character Movement
    private void Update()
    {
        if (canMove)
        {
            // Keep the player inside pitch

            if (!(joystick.Horizantal() > 0 && transform.position.x > 58.5f || joystick.Horizantal() < 0 && transform.position.x < -58.5f ||
                  joystick.Vertical() < 0 && transform.position.z < -103.5f))
            {
                // This check prevents errors when game is resumed from paused menu.
                if (Time.deltaTime == 0)
                {
                    return;
                }

                // Move Character
                transform.position += new Vector3(0, 0, joystick.Vertical() / (Time.deltaTime * 1000));
                transform.position += new Vector3(joystick.Horizantal() / (Time.deltaTime * 1000), 0, 0);

                // Move Ball
                ball.transform.position += new Vector3(joystick.Horizantal() / (Time.deltaTime * 500), 0, 0);
                ball.transform.position += new Vector3(0, 0, joystick.Vertical() / (Time.deltaTime * 500));

                // Rotate Ball
                ball.transform.Rotate(Vector3.forward * (joystick.Horizantal() * -500 * Time.deltaTime));
                ball.transform.Rotate(Vector3.right * (joystick.Vertical() * 500 * Time.deltaTime));

                // Rotate Character and Ball according to Joystick direction
                if (joystick.Horizantal() != 0 || joystick.Vertical() != 0)
                {
                    float angle = Mathf.Atan2(joystick.Horizantal(), joystick.Vertical()) * Mathf.Rad2Deg;
                    transform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));
                }
                Vector3 newBallPosition = transform.position + transform.forward * 1.1f;

                // place the ball in front of player
                newBallPosition.y       = 1.2f;
                ball.transform.position = newBallPosition;

                // Set up animation according to movement
                anim.SetFloat("Horizantal", joystick.Horizantal());
                anim.SetFloat("Vertical", joystick.Vertical());
            }
        }
    }