我試圖用刷卡檢測來扔籃球。 根據滑動的長度將決定施加的力量(距離球被投擲。)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);
}
}
你根本沒有在AddForce中使用長度變量!你錯過了什麼 –
這是我第一次使用AddForce,什麼是正確的語法? @SurajS – Lynnstrum
語法無誤!我的意思是你正在計算'長度'的價值,但沒有在任何地方使用!也許試試rbody.AddForce(new Vector3(touchPosition.x,0,touchPosition.y)* length); –