Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My ball can jump more than once. How to fix it withing this code?

Discussion in 'Scripting' started by Dulas, Jun 19, 2015.

  1. Dulas

    Dulas

    Joined:
    May 31, 2015
    Posts:
    15
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.     public Transform prefab;
    11.  
    12.     private Rigidbody rb;
    13.     private int count;
    14.  
    15.     void Start ()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.         count = 0;
    19.         SetCountText ();
    20.         winText.text = "";
    21.     }
    22.  
    23.     void FixedUpdate ()
    24.     {
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.  
    28.         float jumpers = 0;
    29.         if (Input.GetKeyDown (KeyCode.Space))
    30.         {
    31.             jumpers = 15.0f;
    32.         }
    33.  
    34.         Vector3 movement = new Vector3 (moveHorizontal, jumpers, moveVertical);
    35.  
    36.         rb.AddForce(movement * speed);
    37.  
    38.     }
    39.  
    40.     void OnTriggerEnter(Collider other)
    41.     {
    42.         if(other.gameObject.CompareTag("Pickup"))
    43.         {
    44.             other.gameObject.SetActive (false);
    45.             Instantiate (, transform.position, transform.rotation);
    46.             count = count + 1;
    47.             SetCountText ();
    48.         }
    49.     }
    50.  
    51.     void SetCountText ()
    52.     {
    53.         countText.text = "Count: " + count.ToString ();
    54.         if (count >= 49)
    55.         {
    56.             winText.text = "You win!";
    57.         }
    58.     }
    59. }
    I tried with a private variable isGrounded and used an if statement, yet it still did not work. Can someone give me a hand here, please?
    Thanks in advance.
     
    Last edited: Jun 19, 2015
  2. gavinb80

    gavinb80

    Joined:
    Nov 4, 2012
    Posts:
    96
    you'll be best of wrapping your code in the code tags (there is a icon on the toolbar for "insert" which allows you to do it or use the [code][/code] tags
     
    Last edited: Jun 19, 2015
  3. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Show your script with the isGrounded logic in there. That was the right approach to take, you probably just left something out. You need to be setting your isGrounded flag to true or false depending on whether or not you are touching the ground (a couple different ways to detect this), setting it false otherwise, and only allowing jumps is you are grounded.
     
  4. Dulas

    Dulas

    Joined:
    May 31, 2015
    Posts:
    15
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.  
    11.     private Rigidbody rb;
    12.     private int count;
    13.     private bool isGrounded = true;
    14.  
    15.     void Start ()
    16.     {
    17.         rb = GetComponent<Rigidbody>();
    18.         count = 0;
    19.         SetCountText ();
    20.         winText.text = "";
    21.     }
    22.    
    23.     void FixedUpdate ()
    24.     {
    25.         float moveHorizontal = Input.GetAxis ("Horizontal");
    26.         float moveVertical = Input.GetAxis ("Vertical");
    27.  
    28.         float jumpers = 0;
    29.         if (isGrounded)
    30.         {
    31.             if (Input.GetKeyDown (KeyCode.Space))
    32.             {
    33.                 jumpers = 15.0f;
    34.             }
    35.         }
    36.  
    37.         Vector3 movement = new Vector3 (moveHorizontal, jumpers, moveVertical);
    38.  
    39.         rb.AddForce(movement * speed);
    40.  
    41.     }
    42.  
    43.     void OnTriggerEnter(Collider other)
    44.     {
    45.         if(other.gameObject.CompareTag("Pickup"))
    46.         {
    47.             other.gameObject.SetActive (false);
    48.             count = count + 1;
    49.             SetCountText ();
    50.         }
    51.     }
    52.  
    53.     void SetCountText ()
    54.     {
    55.         countText.text = "Count: " + count.ToString ();
    56.         if (count >= 49)
    57.         {
    58.             winText.text = "You win!";
    59.         }
    60.     }
    61. }