2
\$\begingroup\$

I made this game called Jumpol, about you trying to jump over as many obstacles and collect as many coins as possible. The coins can be used to change your ball and revive yourself so you get another shot on the track.

I've released this game on the iOS and Android apps stores and some users are reporting that the jumping is laggy and feels slow.

I tested the game on my 4 year old Android and it ran fine, plus I tested it on a few iOS devices and it also works fine. Is there any ways to make this code run smoother?

Links to game (If you want to test)

Android Link

iOS Link

float speed = 12;                // Speed of player.
float thrust = 34;              // Jump force.
float gravity = -6.18f;         // Gravity force.

bool isGrounded = false;        // If on ground or not.

private void Awake () {

    Instance = this;

}

private void Start () {

    rb = GetComponent<Rigidbody> ();

}

private void Update () {

    if (Application.platform == RuntimePlatform.WindowsEditor) DesktopControls ();

    if (Application.platform == RuntimePlatform.IPhonePlayer ||
        Application.platform == RuntimePlatform.Android) MobileControls ();

}

private void FixedUpdate () {

    if (GameManager.Instance.started) {

        Gravity ();
        Move ();
    }

}

private void OnCollisionEnter (Collision collision) {

    if (collision.gameObject.tag == "Obstacle") {

        gameObject.SetActive (false);

    }

    if (collision.gameObject.tag == "Ground") {
        isGrounded = true;
    }

}

private void OnCollisionExit (Collision collision) {

    if (collision.gameObject.tag == "Ground") {
        isGrounded = false;
    }

}

private void OnTriggerEnter (Collider other) {

    if (other.gameObject.tag == "Collectable") {

        other.gameObject.SetActive (false);

    }

}

/// <summary>
/// Controls for mobile devices.
/// </summary>
private void MobileControls () {

    for (int i = 0; i < Input.touchCount; ++i)
        if (Input.touchCount > 0 && Input.GetTouch (i).phase == TouchPhase.Began) Jump ();

}

/// <summary>
/// Controls for desktops.
/// </summary>
private void DesktopControls () {

    if (Input.GetMouseButtonDown (0)) Jump ();

}

/// <summary>
/// Makes player jump.
/// </summary>
private void Jump () {

    if (isGrounded) {
        rb.velocity += new Vector3 (0, thrust, 0);
    }

}

/// <summary>
/// Adds gravity.
/// </summary>
private void Gravity () {

    rb.velocity += new Vector3 (0, gravity, 0);

}

/// <summary>
/// Moves player.
/// </summary>
private void Move () {

    rb.velocity = new Vector3 (rb.velocity.x, rb.velocity.y, speed);

}
\$\endgroup\$
2
\$\begingroup\$

This would be a comment but I can't comment yet.

Why do you program the gravity yourself? Why not let the RB handle it? It has also a script-accessible value to change the gravity force. And the Direction of the Gravity is always -y. I think under the hood it could run smoother.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy