我试图用刷卡检测来扔篮球。 根据滑动的长度将决定施加的力量(距离球被投掷。)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); –