2014-09-22 51 views
0

我正在爲移動設備製作2D平臺遊戲。我有一個guiTexture,當按下時,讓我的精靈跳躍。但是每當我按下guiTexture時,精靈就會每次跳到不同的高度,這取決於我按住按鈕還是輕輕按下它。團結在不同高度跳躍

這裏是我使用的腳本跳:

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)); 
     } 
    } 
} 

回答

0

讓您若對touchphase語句檢查觸摸是否下降,那麼它只會當你降落

if (grounded && jumpButton.HitTest(Input.GetTouch(0).position) && Input.GetTouch(0).phase == TouchPhase.Began) { 
     rigidbody2D.AddForce(new Vector2(0, jumpForce)); 
    } 
一次開火