Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by yepez7 · May 26, 2016 at 01:54 PM · mobilejumpholdholding

How can a make on click jump small but if a person hold button jump higher. is for mobile.

How can a make on click jump small but if a person hold button jump higher. im making a 2d platform game for android. I want to make my player jump if user click the button but if he holds the button jump higher.

Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · May 26, 2016 at 03:21 PM 0
Share

Here you go $$anonymous$$ulti Touch Tutorial.

Now concentrate on the deltaTime of the touch event. Record the deltaTime whilst and then run your jump when the touch phase ends.

you'll have to test what works for your game but if you pass the last recorded deltaTime once the touch phase ends you check if it's longer than X amount of time and jump higher if longer.

avatar image yepez7 · May 27, 2016 at 04:50 AM 0
Share

how can i Test deltaTime?

avatar image yepez7 yepez7 · May 27, 2016 at 05:11 AM 0
Share

what I'm doing wrong help???

void OnFirstTouchBegan() { Touch myTouch = Input.GetTouch (0); switch (buttonType) { case type.JumpButton:

             if (playerObject.GetComponent<Player$$anonymous$$anager> ().onGround) 
         {

                 if (myTouch.deltaTime <= 0f) 
                 {
                     Debug.Log ("small");
                     playerRigidbody.AddForce (Vector2.up * jumpHeight, Force$$anonymous$$ode2D.Impulse); // this make the object jump  // esto checa si esta en el suelo o no pa ver si brinca o no
                 }
             if (myTouch.deltaTime > 1f) 
                     
                     {
                         //Long tap
                         Debug.Log ("big");
                         playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, Force$$anonymous$$ode2D.Impulse); // this make the object jump  // esto checa si esta en el suelo o no pa ver si brinca o no

                     }

                 
             }
         break;
         }

     }
avatar image Mmmpies yepez7 · May 27, 2016 at 09:40 AM 0
Share

If Touch.deltaTime is greater than zero store it in a float (e.g. myTimeDeltaTime). I haven't got access to Unity to test this but on touch end read that variable ins$$anonymous$$d of deltaTime.

It's very likely that once the touch has ended the touch.deltaTime will be zero so you have to store it in another variable.

avatar image yepez7 · May 27, 2016 at 01:04 PM 0
Share

no is not working? and i appreciate ur help

i change it to greater

if (myTouch.deltaTime >= 0f)

it only work the small one

avatar image Mmmpies yepez7 · May 27, 2016 at 01:11 PM 0
Share

I'm not sure you understood what I meant so I'll try and explain with what code I can throw together without having access to Unity.

 private float myTouchDT;
 
 void Update()
 {
      if(Touch.deltaTime > 0) { myTouchDT = Touch.deltaTime; }

then when you call the jump check myTouchDT do whichever jump is relevant and set myTouchDT float back to zero. Does that make sense?

avatar image yepez7 · May 27, 2016 at 01:32 PM 0
Share

I'm sorry no

avatar image Mmmpies yepez7 · May 27, 2016 at 01:42 PM 0
Share
 if (myTouchDT >= 1.0f)  // elegir la longitud de tiempo
                  {
                      playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, Force$$anonymous$$ode2D.Impulse);    // GRANDE ;)
                  }
                  else if (myTouchDT > 0.0f) 
                  {
                      playerRigidbody.AddForce (Vector2.up * jumpHeight, Force$$anonymous$$ode2D.Impulse);     // pequeña
                  }

Almost forgot

 myTouchDT = 0; // set to zero after the jump
avatar image yepez7 · May 27, 2016 at 01:57 PM 0
Share

How I put the variable my touchDt on top??

avatar image Mmmpies yepez7 · May 27, 2016 at 02:09 PM 0
Share

Now I don't understand what you mean! Do you want to post you entire script or is too big?

EDIT as we've got a lot of replies, yes if you want.

 private float myTouchDT = 0;

Is fine for setting it initially.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Mmmpies · May 29, 2016 at 01:54 PM

Ah man what a massive pain that was! Spent an hour downloading all the Android SDK stuff then another hour trying to get my PC to recognise my phone! Apparently a known issue with some Samsung phones, in the end I just went back to my laptop!

Anyway it looks like Touch.deltaTime was a bad call, it acts real strange so I went back to the more dependable Time.deltaTime, this script works though...

 public class ButtonMovement : MonoBehaviour {
 
     public Rigidbody2D playerRigidbody;
     private float jumpHeight = 2f;
     private Touch touch;
     private bool isGrounded;
     //public Text myText;
 
     private float myTouchDT = 0f;
 
     // Use this for initialization
     void Start () {
         //myText.text = "Started";
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.touchCount > 0) {
             touch = Input.GetTouch(0);
             myTouchDT = myTouchDT + Time.deltaTime;
             //myText.text = myTouchDT.ToString();
         }
 
         if (touch.phase == TouchPhase.Ended && myTouchDT > 0f && isGrounded) {
             if (myTouchDT < 1.0f) {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight, ForceMode2D.Impulse);
                 //myText.text = ("Samll = " + myTouchDT.ToString());
         
             } else if (myTouchDT >= 1.0f) {
                 playerRigidbody.AddForce (Vector2.up * jumpHeight * 6, ForceMode2D.Impulse);
                 //myText.text = ("Big = " + myTouchDT.ToString());
             }
             myTouchDT = 0f;
         }
     }
 
     void OnCollisionEnter2D(Collision2D col)
     {
         if (col.gameObject.name == "ground")
             isGrounded = true;
     }
 
     void OnCollisionExit2D(Collision2D col)
     {
         if (col.gameObject.name == "ground")
             isGrounded = false;
     }
 }

You can ignore the Text stuff I just used it for debugging on the actual phone.

EDIT - forgot to say your ground needs to be called ground for the collision detection to work properly.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yepez7 · May 31, 2016 at 01:02 AM 0
Share

thank you it did work. but if i hold the left button or right with jump button it only jump high not small jump

avatar image Mmmpies yepez7 · May 31, 2016 at 08:08 AM 0
Share

The problem is this this is a different question to the one you asked. You need to specify a long or short tap and also identify movement.

Normally this is done by specifying a location for each one. Typically you'd want to specify an area based on a proportion of the screen height/width and have a jump/move image there.

It looks like you have a left or right button so you must already do this. Really though you need to ask another question if you can't work it out yourself as the question of small/big jump is answered.

EDIT - I'm not having a go at you it just keeps answers neater if the exact question gets answered :)

Plus you keep posting on Reply, it's badly worded by Unity as Reply on Unity Answers means Answer so if you're just commenting use Add Comment (or, confusingly, reply to another comment).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how can my player keep jumping wen it hit ground again? 0 Answers

help with multitouch buttons 0 Answers

Mario jump addforce problem 0 Answers

Do something when holding finger on screen mobile 0 Answers

Delayed jump on android 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges