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

iPhone project, touch the screen for jumping, the script doesn't work

Discussion in 'Scripting' started by La-Bitt-A-Tibit, Aug 14, 2017.

  1. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Hello,

    I have create a Script if normally I touch the screen, the character jump. But the script doesn't work correctly. The character don't jump :(

    Here's the script in C#:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class Jump : MonoBehaviour
    6. {
    7.      private bool onGround;
    8.      private float jumpPressure;
    9.      private float minJump;
    10.      private float maxJumpPressure;
    11.      private Rigidbody2D rbody2d;
    12.      // Use this for initialization
    13.      void Start()
    14.      {
    15.          onGround = true;
    16.          jumpPressure = 0f;
    17.          minJump = 2f;
    18.          maxJumpPressure = 10f;
    19.          rbody2d = GetComponent<Rigidbody2D>();
    20.      }
    21.      // Update is called once per frame
    22.      void Update()
    23.      {
    24.          if (onGround)
    25.          {
    26.              if (Input.GetTouch(0).phase == TouchPhase.Began)
    27.              {
    28.                  if (jumpPressure < maxJumpPressure)
    29.                  {
    30.                      jumpPressure += Time.deltaTime * 10f;
    31.                  }
    32.                  else
    33.                  {
    34.                      jumpPressure = maxJumpPressure;
    35.                      print(jumpPressure);
    36.                  }
    37.              }
    38.                  else
    39.                  {
    40.                  if (jumpPressure > 0f)
    41.                  {
    42.                      jumpPressure = jumpPressure + minJump;
    43.                      rbody2d.velocity = new Vector3(jumpPressure / 10, jumpPressure, 0f);
    44.                      jumpPressure = 0f;
    45.                      onGround = false;
    46.                  }
    47.              }
    48.          }
    49.      }
    50.      private void OnCollisionEnter2D(Collision2D other)
    51.      {
    52.          if(other.gameObject.CompareTag("Ground"))
    53.          {
    54.              onGround = true;  
    55.          }
    56.      }
    57. }
    What I suppose to scripting for it work?

    Thank You for your help

    sorry for my English, I'm French-Canadian and I don't have the habitude to write text in English... ;)
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Put a print statement before your Velocity line, see if it's getting there.
     
  3. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    I think this part of code will be executed only once

    Code (CSharp):
    1. if (Input.GetTouch(0).phase == TouchPhase.Began)
    2.              {
    3.                  if (jumpPressure < maxJumpPressure)
    4.                  {
    5.                      jumpPressure += Time.deltaTime * 10f;
    6.                  }
    7.                  else
    8.                  {
    9.                      jumpPressure = maxJumpPressure;
    10.                      print(jumpPressure);
    11.                  }
    12.              }
    https://docs.unity3d.com/ScriptReference/TouchPhase.html
     
  4. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Thanks you two, I appreciate. :)
    I'm going to make the correctives. :)
    more details later! :)
     
  5. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    It's work, but, my character jump without he stopping and I want to he return to her initial position on the scene when the jump is finish.

    How do this?

    I have an error in the console too :

    How Fix It?

    Here's my script with last modification:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Jump : MonoBehaviour
    6. {
    7.     private bool onGround;
    8.     private float jumpPressure;
    9.     private float minJump;
    10.     private float maxJumpPressure;
    11.     private Rigidbody2D rbody2d;
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         onGround = true;
    16.         jumpPressure = 0f;
    17.         minJump = 2f;
    18.         maxJumpPressure = 3f;
    19.         rbody2d = GetComponent<Rigidbody2D>();
    20.     }
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (onGround)
    25.         {
    26.             if (Input.GetTouch(0).phase == TouchPhase.Began)
    27.             {
    28.                 if (jumpPressure < maxJumpPressure)
    29.                 {
    30.                     jumpPressure += Time.deltaTime * 3f;
    31.                     print(jumpPressure);
    32.                 }
    33.                 else
    34.                 {
    35.                     jumpPressure = maxJumpPressure;
    36.                     print(jumpPressure);
    37.                 }
    38.             }
    39.             else
    40.             {
    41.                 if (jumpPressure > 0f)
    42.                 {
    43.                     jumpPressure = jumpPressure + minJump;
    44.                     print(jumpPressure);
    45.                     rbody2d.velocity = new Vector3(jumpPressure / 3, jumpPressure, 0f);
    46.                     jumpPressure = 0f;
    47.                     onGround = false;
    48.                 }
    49.             }
    50.         }
    51.     }
    52.     private void OnCollisionEnter2D(Collision2D other)
    53.     {
    54.         if (other.gameObject.CompareTag("Ground"))
    55.         {
    56.             onGround = true;
    57.         }
    58.     }
    59. }
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    For the error, you'll likely want to check that GetTouch(0) is a valid reference. Where in the code when you step through it by hand, do you expect the jumping to stop? As mentioned, Touchphase.Began may only happen once.
     
  7. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Visual Studio's console say is a valid reference but Unity's console say it's not valid so I don't know why the GetTouch(0) is not valid. Can I've help for than I can understand?

    For stopping jump, I'm want help too!

    Sorry if I ask a lot of questions, I'm new in the Unity Universe! :)
     
  8. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Because you're asking it what the phase of the touch is, even when there is no touch going on. Make sure there is at least one finger touching the screen before you ask what the phase is.
    Code (CSharp):
    1. if (Input.touchCount > 0)
     
  9. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
  10. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    My project is 2D so this script doesn't work, I have modified "Rigidbody" to "Rigidbody2D", "ForceMode" to "ForceMode2D" and add "(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)" to touching the screen for jumping character. The character doesn't jump.

    here the new script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody2D))]
    5. public class Jump : MonoBehaviour
    6. {
    7.  
    8.     public Vector3 jump;
    9.     public float jumpForce = 2.0f;
    10.  
    11.     public bool Ground;
    12.     Rigidbody2D rb;
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.         jump = new Vector3(0.0f, 2.0f, 0.0f);
    17.     }
    18.  
    19.     void OnCollisionStay()
    20.     {
    21.         Ground = true;
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    27.         {
    28.             rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
    29.             Ground = false;
    30.         }
    31.     }
    32. }
    33.  
    What do for fix it?

    Thanks for your help :)
     
  11. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    What is the value of jumpForce in the inspector?
     
  12. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Here's the informations :
     
  13. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Can you take another screenshot of your rigidbody component with all its options visible, in the inspector, as well?
     
  14. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Here's RigidBody2D:
     
  15. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Le problème c'est que ton rigidbody est set a Kinematic. Kinematic est pas affecté par les forces et la gravité. Set le a dynamic et ça devrait fonctionner!
     
  16. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Ok, Je vais voir ce que ça donne, merci! :)
     
  17. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Ça ne marche pas, il ne veut toujours pas sauter. :(

    Que faire?
     
  18. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Peux tu prendre un nouveau screenshot de ton rigidbody, une fois set a dynamic? Je vais essayer de reproduire ton problème de mon coté

    Edit : Dis moi aussi si tu reçois des erreurs dans la console? Et si tu utilises bien ton doigt et non la souris pour tester?
     
  19. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Je reçois 0 erreur dans la console et j'utilise mon iPhone qui est branché sur mon Mac pour le Unity Editor:


     
  20. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Est-ce que une de tes animations dans ton animator bouge la position du personage? Si oui, met l'animator a off (avec la checkbox) et regarde si ton saut fonctionne.

    Quand tu dis que tu utilises ton iPhone, tu veux bien dire Unity Remote?

    De mon cote, ton setup fonctionne bien.

    Edit : Click la checkbox de ton rigidbody a "simulated"
    De plus, tu veux probablement augmenter la masse, sinon ta tortue va explorer l'espace
     
  21. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Ma tortue est immobile, elle fait du surplace. J'ai une animation de la tortue qui fait assemblant de marcher. Le décor bouge via un script. J'utilise Unity Remote. ;)

    Ça ne marche toujours pas :(
     
  22. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    T'as bien mis ton rigidbody a simulated?
     
  23. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
  24. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Je pense que le problème est le script...
     
  25. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Ajoute ca dans ton update et dit moi si la console l'imprime
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    4.         {
    5.             Debug.Log ("Hi");
    6.             rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
    7.             Ground = false;
    8.         }
    9.     }
     
  26. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Oui, la console imprime (J'ai touché 34 fois)
     
  27. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Tu m'as bien dit que tu avais essaye de disable l'animator et toujours rien?
     
  28. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Je ne sais pas comment faire, je ne l'ai pas fait.
    Comment faire? :)
     
  29. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Sur ton objet "Player", dans l'inspecteur, clique la boite de la composante "Animator" pour enlever le crochet. Ou bien enleve l'animator tout simplement.
     
  30. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    J'ai enlevé l'Animator du Player, mais, ça ne fonctionne toujours pas :(
     
  31. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Ton script et ton rigidbody sont bien setup, ils fonctionnent je t'assure. Je dois demander, est-ce que ton script "jump" est bel et bien sur ton objet "Player" et non sur un autre objet?
     
  32. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Le script est bel et bien sur mon "Player"
     
  33. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    is there any collider attached to the water ?
     
  34. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    Yes, I have boxes colliders on my 2 waters spots! :)
     
  35. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    990
    Remove them!
     
  36. La-Bitt-A-Tibit

    La-Bitt-A-Tibit

    Joined:
    Aug 14, 2017
    Posts:
    19
    It's Work!!! :) WORK WORK WORK!!! :)
    Thanks!!! :)
     
    ADNCG likes this.
  37. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    you cant jump through the roof :)

    use collider only when needed, and marked as a static if it doesnt move