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.
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.