2017-07-03 78 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

您还可以添加公共浮动力;在你的班级,并从编辑器中更改它的易用性 –