Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I wrote a simple script to control my 'Player' object:

using UnityEngine;
 using System.Collections;

 public class PlayerMovement : MonoBehaviour {

     public float jumpVelocity = 10;

     private Transform groundCheck;
     private Animator anim;
     private Rigidbody2D rigidbody2D;
     private bool grounded;
     private Physics2D gravity;

     [SerializeField] private LayerMask whatIsGround;

     const float groundedRadius = .2f;


     void Awake () {

         // Setting up references.
         groundCheck = transform.Find("GroundCheck");
         anim = GetComponent<Animator>();
         rigidbody2D = GetComponent<Rigidbody2D>();

     }

     // Use it for Graphic & Input
     void Update () {

     }

     //Physics and ground check
     void FixedUpdate () {

         grounded = false;

         Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, groundedRadius, whatIsGround);
         for (int i = 0; i < colliders.Length; i++)
         {
             if (colliders[i].gameObject != gameObject)
                 grounded = true;
         }

     }

     //Jump function
     public void Jump () {

         if (grounded) 
             rigidbody2D.velocity = jumpVelocity * Vector2.up;    
     }

     //Gliding function
     public void GlideON () {

         rigidbody2D.gravityScale = 1;
         Debug.Log ("Graviti is on");
     }

     public void GlideOFF () {

         rigidbody2D.gravityScale = 0;
         Debug.Log ("Graviti is off");

         if (!grounded) {
             rigidbody2D.velocity = ( -jumpVelocity / 10 ) * Vector2.up;
         }
     }

 }

To operate touch screen I used GUI function like this:

enter image description here

What I want to achieve:

  • One click - Player jump
  • Hold button - Player starts glide.
  • Player can not glide when he's grounded.
  • It does not matter whether I start from 'PointerClick' or 'PointerDown'. If player is grounded the first action should be 'Jump' always.
  • If we start with 'PointerDown', Player should jump and after reaching the maximum height he starts glide.
  • If we start with 'PointerClick' Player should jump.
  • If Player is not grounded and we want to start glide - we have to touch screen and hold it.

Where is the problem:

  • When I start with 'PointerDown', my Player begins infinite jump until 'PointerUp'.

I would be grateful for your help and suggestions.

share|improve this question

1 Answer 1

You could remedy to this by adding another bool canJump :

public void Jump () {
       if(canJump) {
         if (grounded) 
             rigidbody2D.velocity = jumpVelocity * Vector2.up;
       canJump = false;    
    }
 }

So when you are clicking, the Jump() method will run the jumping loop one time.After this code, you will need to set the canJump boolean to true once he hits the ground again.

EDIT : By the way, using Event handlers isn't the most effective way you could use to handle Android/iOs touch events. You can simply add code to your script using Input.GetTouch which offers not just 2 phases (Down, Up) but 4-(5) phases of "Touching".

share|improve this answer
    
It's half - solution because still don't work exactly as I want...but it's next step forward. Thank you for showing the direction of development. –  MaD May 19 at 19:35
    
Hahah! I clearly said "After this code, you will need to set the canJump boolean to true once he hits the ground again." So that's the only other half left. You need to write down something by your own no? :) Especially if it's this easy –  Zee May 19 at 19:38
    
Even if its calling the function over and over it shouldn't jump repeatedly if your character isn't grounded, so I would take a look at your grounded code in FixedUpdate and see just when your character is being reported as grounded. You can also control the repeatDelay via the InputModule, setting it to 0.5f or so will give your character enough time to leave the ground before its called again. –  Jeremiah Leslie Jun 17 at 22:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.