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

Ball jump issue

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

  1. Dulas

    Dulas

    Joined:
    May 31, 2015
    Posts:
    15
    I have this code:
    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.  
    9.     private Rigidbody rb;
    10.     private bool isGrounded;
    11.  
    12.     void Start ()
    13.     {
    14.         rb = GetComponent<Rigidbody> ();
    15.         isGrounded = true;
    16.     }
    17.    
    18.     void FixedUpdate ()
    19.     {
    20.         float moveHorizontal = Input.GetAxis ("Horizontal");
    21.         float moveVertical = Input.GetAxis ("Vertical");
    22.  
    23.         float jumpers = 0;
    24.         if (isGrounded)
    25.         {
    26.             if (Input.GetKeyDown (KeyCode.Space))
    27.             {
    28.                 jumpers = 15.0f;
    29.                 isGrounded = false;  
    30.             }
    31.         }
    32.  
    33.         Vector3 movement = new Vector3 (moveHorizontal, jumpers, moveVertical);
    34.  
    35.         rb.AddForce(movement * speed);
    36.  
    37.     }
    38.  
    39.     void OnTriggerEnter(Collider other)
    40.     {
    41.         if(other.gameObject.CompareTag("Pickup"))
    42.         {
    43.             other.gameObject.SetActive (false);
    44.  
    45.         }
    46.         if (other.gameObject.CompareTag ("Floor"))
    47.         {
    48.             isGrounded = true;
    49.         }
    50.     }
    51. }
    The problem is that I can only jump once. To state the obvious, all the floors are given the proper tag "Floor". How can I fix this? Thanks.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    It seems unlikely to me that your floors are triggers, are you sure you don't want OnCollisionEnter instead of OnTriggerEnter?

    Add some Debug.Log() statements and make sure your if statements are firing as expected.
     
    Dulas likes this.
  3. Dulas

    Dulas

    Joined:
    May 31, 2015
    Posts:
    15
    Wow I'm ashamed I missed that one out. Yep it worked the way you said, thanks a lot pal, you saved me some precious time :D
     
    GroZZleR likes this.