I'm making a 2D platforming game for mobile devices. I have a guiTexture, that when pressed, makes my sprite jump. But whenever I press the guiTexture, the sprite jumps at different heights every time, depending if I hold the button down, or lightly press it.
Here is the jump script I'm using:
using UnityEngine;
using System.Collections;
public class Jump : MonoBehaviour {
public GUITexture jumpButton;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
public GameObject character;
void Start () {
}
void Update () {
}
void FixedUpdate () {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
if (grounded && jumpButton.HitTest(Input.GetTouch(0).position)) {
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
}
make your if statement check for the touchphase if the touch is down, then it will only fire once when you touch down
if (grounded && jumpButton.HitTest(Input.GetTouch(0).position) && Input.GetTouch(0).phase == TouchPhase.Began) {
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}