3
\$\begingroup\$

I'm making a very simple game with a sphere that's supposed to roll and bounce on "planets". Also, please be ready to read a lot. I want to be very descriptive for you. :)

I've pretty-much-done everything for the game already, except I have a problem with jumping. When I press the space bar, the sphere will hop once. But after pressing space again, it will not.

What We're Dealing With

Now this sounds like an easy fix, but keep in mind that the player object was designed with the mesh (rendered) Player_Ball, and the jump collider named Player_JumpBox, which is just an invisible collider smaller than Player_Ball. My idea was that when an object with a "static" tag is the object the Player_JumpBox collides with (which is a trigger), it will enable the user to jump. So this meant I had to make two different classes. One for the parent object, that holds both Player_Ball and Player_JumpBox, and one for the Player_JumpBox titled "JumpControl.cs", so it could handle the Player_JumpBox OnTriggerEnter event.

The player ball.

The JumpControl.cs file located on the Player_JumpBox object, takes input from the user, and controls when to jump. (Note: I did not say how to jump)

The PlayerController.cs file located on the parent Player object, controls everything about the player. It controls how to jump. (Basically has variables like jumpForce and a function that JumpControl.cs uses to tell the parent object to jump.)

The PlayerController.cs file for the Player object.

The Scripts (C#)

JumpControl.cs

using UnityEngine;
using System.Collections;

public class JumpControl : MonoBehaviour {

    public GameObject player;   // The parent object, holding both 

    private bool canJump;

    void Start () {
        canJump = true;
    }

    // Every physics update, check if the user is pressing `space', and if so, jump, then set 'canJump = false'.
    void Update () {
        if (Mathf.Abs(Input.GetAxis("Jump")) > 0.1 && canJump == true) {
            player.GetComponent<PlayerController> ().Jump ();
            canJump = false;
            print ("DEBUG: canJump = " + canJump);
        }
    }

    // When the user nears the ground, enable them to jump again.
    void OnTriggerEnter (Collider c) {
        if (c.CompareTag("Static")) {
            canJump = true;
            print ("DEBUG: canJump = " + canJump);
        }
    }
}

PlayerController.cs

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;                 // The player's rolling speed.
    public int maxCoins;                // The number of coins in the level.
    public Text counter;                // The text that displays the number of coins the player has.
    public Text finalText;              // The text that displays win-text.
    public bool isTutorial;             // Whether or not this is a tutorial.
    public float jumpForce;             // The force of jumping.

    private Rigidbody rb;               // The rigidbody.
    private float moveVertical;
    private float moveHorizontal;
    private int coins;                  // Number of coins the player has.

    void Start () {
        rb = gameObject.GetComponent<Rigidbody> ();
        coins = 0;
        UpdatePoints ();
        finalText.text = "";
    }

    void Update() {
        moveHorizontal = Input.GetAxis ("Horizontal");
        moveVertical = Input.GetAxis ("Vertical");
    }

    void FixedUpdate () {
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider c) {
        if (c.gameObject.CompareTag ("Coin")) {
            c.gameObject.SetActive (false);
            coins++;
            UpdatePoints ();
        }
    }

    /// <summary>
    /// Updates points the user has, and displays them on the screen.
    /// </summary>
    void UpdatePoints() {
        if (coins < maxCoins) {
            counter.text = "Coins: " + coins;
        } else {
            counter.text = "Coins: " + coins;
            if (isTutorial) {
                finalText.text = "Tutorial complete!";
            } else {
                finalText.text = "You win!";
            }
        }
    }

    /// <summary>
    /// The method JumpControl.cs uses to make the player jump.
    /// </summary>
    public void Jump() {
        Vector3 jump = new Vector3 (0.0f, jumpForce);
        rb.AddForce (jump);
        print ("Jumped");
    }
}

(The only truly important thing is the Jump method at the bottom of PlayerController.cs)

What I Want

I really just want to know why the player isn't bouncing everytime I press 'space'. It's really bam-boozled me for a while, and I really need your help.

Thanks for reading! I tried to make it easy to understand.

\$\endgroup\$
  • \$\begingroup\$ For those of the people that want to see what I mean in action, view this link. I'm attempting to jump to the last planet, but I am unable to do so. (Note: The log is at the bottom of the screen) \$\endgroup\$ – VenHayz Jul 14 '16 at 4:58
0
\$\begingroup\$

Silly me, I assumed I already made the Player's Rigidbody a trigger.

Also, you need to make each and every child of the planet (like the tops of the planet) that you want to let the player bounce on to be static; not just the empty object parent.

I'm very sorry I never double-checked, but I hope this helps others out.

\$\endgroup\$
  • 3
    \$\begingroup\$ Might be worth marking your question as answered, just to avoid confusion :) \$\endgroup\$ – hobnob Jul 14 '16 at 8:09
  • \$\begingroup\$ @hobnob I can't, because I need to accept it tomorrow. lol, I would have. \$\endgroup\$ – VenHayz Jul 14 '16 at 23:06
  • 2
    \$\begingroup\$ It's now a little bit past tomorrow. Could you accept this now, @VenHayz? \$\endgroup\$ – Tyyppi_77 Sep 20 '16 at 10:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.