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

Question Why or how do i make this code work

Discussion in 'Getting Started' started by MYM_Chapa, Dec 6, 2022.

  1. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    I was watching a tutorial on Youtube and I copied the code exactly the same but it gave me errors and I solved them but the code to jump doesn't work, I'm very new in Unity.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControler : MonoBehaviour {
    6.       bool canJump;
    7.  
    8.  
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if(Input.GetKey("left"))
    21.         {
    22.             gameObject.transform.Translate(-50f * Time.deltaTime, 0, 0);
    23.         }
    24.         if(Input.GetKey("right"))
    25.         {
    26.             gameObject.transform.Translate(50f * Time.deltaTime, 0, 0);
    27.         }
    28.  
    29.     }
    30.  
    31. // i think this is the part of the code that doesnt work
    32.  
    33. void ManageJump()
    34. { if(gameObject.transform.position.y <= 0)
    35.     {
    36.         canJump = false;
    37.     }
    38.     if (Input.GetKey("up") && canJump && gameObject.transform.position.y < 10)
    39.     {
    40.         gameObject.transform.Translate(0, 50f * Time.deltaTime, 0);
    41.  
    42.     }
    43.     else
    44.     {
    45.         canJump = false;
    46.  
    47.         if (gameObject.transform.position.y < 0)
    48.         {
    49.             gameObject.transform.Translate(0, 50f * Time.deltaTime, 0);
    50.         }
    51.     }
    52. }
    53.  
    54. }
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    509
    It seems a bit unlikely that this is all that was in the tutorial or you didn't finish it yet. Notice you have a method named ManageJump but nothing is actually calling it. Also as far as I can see canJump is never set to true.
     
  3. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    thank u so much bro, i fix the problem thx