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

New here! is there anything wrong with my coding?

Discussion in 'Scripting' started by qL-, Sep 18, 2015.

  1. qL-

    qL-

    Joined:
    Sep 18, 2015
    Posts:
    5
    i used Debug.log to check my S button. The S button is not consistant. when pressed, sometime able to jump, sometime it run at all. not sure where is the problem.

    Code (CSharp):
    1.   public float speed = 2f;
    2.     public  float jump = 2f;
    3.     // Use this for initialization
    4.     void Start() {
    5.  
    6.     }
    7.  
    8.     // Update is called once per frame
    9.     void FixedUpdate() {
    10.         if (Input.GetKey(KeyCode.D)){
    11.             transform.Translate(Vector2.right * speed * Time.deltaTime);
    12.         }
    13.         if (Input.GetKey(KeyCode.A))
    14.         {
    15.             transform.Translate(Vector2.left * speed * Time.deltaTime);
    16.         }
    17.         if (Input.GetKeyDown(KeyCode.S))
    18.         {
    19.             transform.Translate(Vector2.up * jump * Time.deltaTime);
    20.             Debug.Log("S is pressed");
    21.             Debug.Log("jump = "+ jump);
    22.             Debug.Log("Vector2.up = " + Vector2.up);
    23.             Debug.Log("Time.deltaTime = " + Time.deltaTime);
    24.         }
    25.     }
    26. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Do it in Update instead of FixedUpdate
     
    Kiwasi likes this.
  3. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    Input.GetKeyDown() is only true during the single frame that the key was pressed down. You translate the transform in that one frame, but since you multiply the amount by Time.deltaTime (the time that elapsed between the current and previous frame), the amount is very very small. You're supposed to multiply by Time.deltaTime if you animate something over several frames, not in a single frame. On top of that, you shouldn't put that code in FixedUpdate, but in regular Update. FixedUpdate should be used for physics only - it is only called at a fixed time step, meaning that Update() is called more often (at every single frame) and will therefor result in smoother gameplay. The multiplication with Time.deltaTime is necessary in Update() because unlike FixedUpdate(), the time between frames varies; if you didn't factor this in your calculations, your animations would appear erratic.
     
  4. qL-

    qL-

    Joined:
    Sep 18, 2015
    Posts:
    5
    thanks! its working. btw how to make it only can jump once?
     
  5. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    In general, if you want something to only happen once, you need a way for the script to remember that that action has been done, and then prevent it from happening again. You can do this easily by having a bool member variable that's set to true when you jump, and checking that variable before you execute a jump - only execute a jump if there has not been a jump before.
     
  6. qL-

    qL-

    Joined:
    Sep 18, 2015
    Posts:
    5
    hm... okay thank i will try haha
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Plenty if threads around with details in doing ground checks as well.
     
  8. qL-

    qL-

    Joined:
    Sep 18, 2015
    Posts:
    5
    Code (CSharp):
    1. void OnTriggerStay2D(Collider2D col)
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.S) && col.gameObject.tag == "Ground")
    4.         {
    5.             rb2d.AddForce(Vector2.up * jump);
    6.          
    7.         }
    8.     }
    is it possible to use this method? when contact to object that tag with ("Ground" && S) is pressed, it will jump.
    however i tried this, it doesnt work. XD
    anyway i am pretty confuse with Update and FixedUpdate still haha. but i will just use Update() for the time being. it seem more reliable.
     
  9. qL-

    qL-

    Joined:
    Sep 18, 2015
    Posts:
    5
    Changed to:
    Code (CSharp):
    1. void OnCollisionStay2D(Collision2D col)
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.S) && col.gameObject.tag == "Ground")
    4.         {
    5.             rb2d.AddForce(Vector2.up * jump);
    6.        
    7.         }
    8.     }
    its able to jump. but never execute everytime. why?