2017-07-03 84 views
0

我試圖用刷卡檢測來扔籃球。 根據滑動的長度將決定施加的力量(距離球被投擲。)GUI更新,所以我的滑動被檢測到,但籃球根本不動。我試着檢查運動學並取消檢查它,結果相同。 該腳本附加到我的相機。我將球對象拖入了由公共變量創建的對象和剛體位置。 這是我目前使用的代碼;Unity3D球沒有被刷卡

using UnityEngine; 
using System.Collections; 

public class NewBehaviourScript : MonoBehaviour 
{ 

    void Start() 
    { 

    } 

    private float length = 0; 
    private bool SW = false; 
    private Vector3 final; 
    private Vector3 startpos; 
    private Vector3 endpos; 

    public GameObject basketball; //Basketball Obj 
    public Rigidbody rbody;// Basketball Obj 

    void Update() 
    { 
     rbody = GetComponent<Rigidbody>(); 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
     { 
      final = Vector3.zero; 
      length = 0; 
      SW = false; 
      Vector2 touchDeltaPosition = Input.GetTouch(0).position; 
      startpos = new Vector3(touchDeltaPosition.x, 0,    touchDeltaPosition.y); 
     } 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
     { 
      SW = true; 
     } 

     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled) 
     { 
      SW = false; 
     } 

     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) 
     { 
      SW = false; 
     } 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
     { 
      if (SW) 
      { 
       Vector2 touchPosition = Input.GetTouch(0).position; 
       endpos = new Vector3(touchPosition.x, 0, touchPosition.y); 
       final = endpos - startpos; 
       length = final.magnitude; 
       rbody.AddForce(new Vector3(touchPosition.x, 0, touchPosition.y)); 
      } 
     } 
    } 

    void OnGUI() 
    { 
     GUI.Box(new Rect(50, 300, 500, 30), "length: " + length); 
    } 
} 
+0

你根本沒有在AddForce中使用長度變量!你錯過了什麼 –

+0

這是我第一次使用AddForce,什麼是正確的語法? @SurajS – Lynnstrum

+0

語法無誤!我的意思是你正在計算'長度'的價值,但沒有在任何地方使用!也許試試rbody.AddForce(new Vector3(touchPosition.x,0,touchPosition.y)* length); –

回答

0

這可能是問題,rbody引用相機的剛體(如果有的話)

試試這個代碼,並確保籃球有一個剛體部件裝配!

void Update() 
    { 
     rbody = basketball.GetComponent<Rigidbody>(); 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
     { 
      final = Vector3.zero; 
      length = 0; 
      SW = false; 
      Vector2 touchDeltaPosition = Input.GetTouch(0).position; 
      startpos = new Vector3(touchDeltaPosition.x, 0,    touchDeltaPosition.y); 
     } 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) 
     { 
      SW = true; 
     } 

     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled) 
     { 
      SW = false; 
     } 

     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) 
     { 
      SW = false; 
     } 
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) 
     { 
      if (SW) 
      { 
       Vector2 touchPosition = Input.GetTouch(0).position; 
       endpos = new Vector3(touchPosition.x, 0, touchPosition.y); 
       final = endpos - startpos; 
       length = final.magnitude; 
       rbody.AddForce(new Vector3(touchPosition.x, touchPosition.x +touchPosition.y, touchPosition.y)); 
      } 
     } 
    } 
+0

嗯,這工作,sorta。 現在球剛剛消失。我認爲它啓動得太快了。 我使用rbody.AddForce(new Vector3(touchPosition.x,0,touchPosition.y));沒有乘以它。 試過; rbody.AddForce(new Vector3(touchPosition.x,0,touchPosition.y)* 100);發生同樣的事情。 – Lynnstrum

+0

嘗試乘以0.1f的因子並向上工作 –

+0

您還可以添加公共浮動力;在你的班級,並從編輯器中更改它的易用性 –