2016-11-05 70 views
1

我有一个对象,我想通过GetTouch在一定的距离抛出。我的代码正在工作,但问题是当我多次触摸屏幕,对象也移动多次,我不想要的,无论我触摸或滑动屏幕多少次,我都希望我的对象只移动一次。这是我试过的一些东西。第一次触摸后明智的GetTouch

public class RealBallMove : MonoBehaviour { 

public float speed; 
public Rigidbody rb; 


void Start() 
{ 
    rb = GetComponent<Rigidbody>(); 
} 

void Update() 
{ 
    if (Input.touchCount >0 && 
     Input.GetTouch(0).phase == TouchPhase.Ended || (Input.GetMouseButtonDown(0))) 
    { 
     //rb.AddForce(Vector3.forward * speed); 
     //rb.AddForce(Vector3.up * speed); 
     GetComponent<Rigidbody>().isKinematic = false; 

     GetComponent<Rigidbody>().AddForce (new Vector3(0.0f, 20.0f, 12.0f)); 
     //Destroy (GetComponent<Rigidbody>()); 
    } 

} }

回答

2

只需添加一个布尔值,指示是否已将球投进;)

public class RealBallMove : MonoBehaviour { 

    public float speed; 
    public Rigidbody rb; 
    private bool thrown ; 


    void Start() 
    { 
     rb = GetComponent<Rigidbody>(); 
    } 

    void Update() 
    { 
     if (
      !thrown && (
      (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) || 
      Input.GetMouseButtonDown(0)) 
     ) 
     { 
      rb.isKinematic = false; 
      rb.AddForce (new Vector3(0.0f, 20.0f, 12.0f)); 
      thrown = true ; 
     } 
    } 
} 

的另一个选项是在检测到触摸时禁用脚本,但只有你的脚本只是上面的行而没有别的。

+0

非常感谢。 –