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

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 :frowning:

Here’s the script in C#:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jump : MonoBehaviour
{
     private bool onGround;
     private float jumpPressure;
     private float minJump;
     private float maxJumpPressure;
     private Rigidbody2D rbody2d;
     // Use this for initialization
     void Start()
     {
         onGround = true;
         jumpPressure = 0f;
         minJump = 2f;
         maxJumpPressure = 10f;
         rbody2d = GetComponent<Rigidbody2D>();
     }
     // Update is called once per frame
     void Update()
     {
         if (onGround)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 if (jumpPressure < maxJumpPressure)
                 {
                     jumpPressure += Time.deltaTime * 10f;
                 }
                 else
                 {
                     jumpPressure = maxJumpPressure;
                     print(jumpPressure);
                 }
             }
                 else
                 {
                 if (jumpPressure > 0f)
                 {
                     jumpPressure = jumpPressure + minJump;
                     rbody2d.velocity = new Vector3(jumpPressure / 10, jumpPressure, 0f);
                     jumpPressure = 0f;
                     onGround = false;
                 }
             }
         }
     }
     private void OnCollisionEnter2D(Collision2D other)
     {
         if(other.gameObject.CompareTag("Ground"))
         {
             onGround = true;  
         }
     }
}

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… :wink:

Put a print statement before your Velocity line, see if it’s getting there.

I think this part of code will be executed only once

if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 if (jumpPressure < maxJumpPressure)
                 {
                     jumpPressure += Time.deltaTime * 10f;
                 }
                 else
                 {
                     jumpPressure = maxJumpPressure;
                     print(jumpPressure);
                 }
             }

Thanks you two, I appreciate. :slight_smile:
I’m going to make the correctives. :slight_smile:
more details later! :slight_smile:

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:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour
{
    private bool onGround;
    private float jumpPressure;
    private float minJump;
    private float maxJumpPressure;
    private Rigidbody2D rbody2d;
    // Use this for initialization
    void Start()
    {
        onGround = true;
        jumpPressure = 0f;
        minJump = 2f;
        maxJumpPressure = 3f;
        rbody2d = GetComponent<Rigidbody2D>();
    }
    // Update is called once per frame
    void Update()
    {
        if (onGround)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (jumpPressure < maxJumpPressure)
                {
                    jumpPressure += Time.deltaTime * 3f;
                    print(jumpPressure);
                }
                else
                {
                    jumpPressure = maxJumpPressure;
                    print(jumpPressure);
                }
            }
            else
            {
                if (jumpPressure > 0f)
                {
                    jumpPressure = jumpPressure + minJump;
                    print(jumpPressure);
                    rbody2d.velocity = new Vector3(jumpPressure / 3, jumpPressure, 0f);
                    jumpPressure = 0f;
                    onGround = false;
                }
            }
        }
    }
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            onGround = true;
        }
    }
}

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.

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! :slight_smile:

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. if (Input.touchCount > 0)

You probably want to separate the jumping action from the Touch. I would suggest a separate DoJump routine or similar. I see that you might be using the script from here. Did you get the example working, before you tried to make your customizations? Always start with something that works, then add your features Can someone help me make a simple jump script? - Questions & Answers - Unity Discussions

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:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]
public class Jump : MonoBehaviour
{

    public Vector3 jump;
    public float jumpForce = 2.0f;

    public bool Ground;
    Rigidbody2D rb;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        jump = new Vector3(0.0f, 2.0f, 0.0f);
    }

    void OnCollisionStay()
    {
        Ground = true;
    }

    void Update()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
            Ground = false;
        }
    }
}

What do for fix it?

Thanks for your help :slight_smile:

What is the value of jumpForce in the inspector?

Here’s the informations :

Can you take another screenshot of your rigidbody component with all its options visible, in the inspector, as well?

Here’s RigidBody2D:

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!

Ok, Je vais voir ce que ça donne, merci! :slight_smile:

Ça ne marche pas, il ne veut toujours pas sauter. :frowning:

Que faire?

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?

Je reçois 0 erreur dans la console et j’utilise mon iPhone qui est branché sur mon Mac pour le Unity Editor:

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