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. Dismiss Notice

My character can Only Jump Once

Discussion in 'Scripting' started by ke1739578, May 7, 2021.

  1. ke1739578

    ke1739578

    Joined:
    Apr 13, 2021
    Posts:
    5
    Hello, it's me again. I fixed the last error with the help of you guys. Anyways, while I was testing my game, I realized I could only Jump once in my game. I made sure I tagged my baseplate as Ground, and I made sure my script wa right. Yet I couldn't find a solution. Could you guys help me again?
    Here is the code for the jumping.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class PlayerControls : MonoBehaviour
    7.  
    8. {
    9.     public float speed = 15f;
    10.  
    11.     private Rigidbody rb;
    12.  
    13.     public float jumpForce = 14;
    14.  
    15.     public bool cubeIsOnTheGround = true;
    16.  
    17.     public TextMeshProUGUI ScoreText;
    18.     private int Score;
    19.  
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody>();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         PlayerRun();
    31.         if (Score >= 3)
    32.         {
    33.  
    34.             YouWin();
    35.         }
    36.  
    37.     }
    38.  
    39.  
    40.     void YouWin()
    41.     {
    42.         ScoreText.text = "You win";
    43.         Time.timeScale = 0f;
    44.     }
    45.  
    46.  
    47.     void PlayerRun()
    48.     {
    49.  
    50.         float leftRight = Input.GetAxis("Horizontal");
    51.         float forwardBack = Input.GetAxis("Vertical");
    52.         Vector3 CharacterMove = new Vector3(leftRight, 0, forwardBack) * speed * Time.deltaTime;
    53.         transform.Translate(CharacterMove, Space.Self);
    54.  
    55.         if (Input.GetButtonDown("Jump") && cubeIsOnTheGround)
    56.         {
    57.  
    58.             rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    59.             cubeIsOnTheGround = false;
    60.         }
    61.         void OnCollisionEnter(Collision collision)
    62.         {
    63.             Debug.Log("Entered");
    64.             if (collision.gameObject.tag == ("Ground"))
    65.             {
    66.                 cubeIsOnTheGround = true;
    67.  
    68.             }
    69.         }
    70.     }
    71.  
    72.     void OnTriggerEnter(Collider other)
    73.     {
    74.         if (other.gameObject.CompareTag("Item"));
    75.         other.gameObject.SetActive(false);
    76.         AddScore();
    77.     }
    78.  
    79.  
    80.  
    81.  
    82.  
    83.     void AddScore()
    84.     {
    85.         Score++;
    86.         ScoreText.text = Score.ToString();
    87.  
    88.     }
    89. }
     
  2. BenniKo

    BenniKo

    Joined:
    Mar 24, 2015
    Posts:
    100
    I think that OnCollisionEnter(Collision collision) will never be called because you nested it inside PlayerRun().
    I didn't even know that is possible in c# :)
     
  3. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Its a pretty new feature in C#. Itsd actually pretty usable. for things like

    Code (CSharp):
    1. public void OuterMethod(foo)
    2. {
    3.    State GetState()
    4.    {
    5.       if(someCondition) return someState;
    6.       if(someOtherCond) return someOtherState;
    7.      
    8.       return someDefaultState;
    9.    }
    10.  
    11.    var state = GetState();
    12.    ...
    13. }
     
    BenniKo likes this.