0
好吧,我已经将球投入到了球的位置,如果我向右滑动,它会沿着这个方向前进,但这是唯一的方向。 如果我向上滑动或屏幕上的任何位置,球仍然会向右移动。所以我的问题是,我可以在我的代码中更改哪些内容,以便在我滑动的方向上滑动?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
public float Force = 0.2f;
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) * Force);
}
}
}
void OnGUI()
{
GUI.Box(new Rect(50, 300, 500, 30), "length: " + length);
}
}
现在没有应用力。 – Lynnstrum
一旦尝试记录'startPos'和'endPos'值,只是为了检查您是否正在接收正确的值。并且在'AddForce()'函数下面添加'SW = false'也会将变量名'final'改为像fin这样的其他名称。 –
我使用GUI来跟踪开始和结束pos,它们都在更新,因为我在整个屏幕上滑动。 – Lynnstrum