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.
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.
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;
}
}
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.
no is not working? and i appreciate ur help
i change it to greater
if (myTouch.deltaTime >= 0f)
it only work the small one
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?
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
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.
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.
thank you it did work. but if i hold the left button or right with jump button it only jump high not small jump
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
![](https://koobas.hobune.stream/wayback/20220612081626im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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