C# Unity3D球仅在轻扫时向右射门

C# Unity3D球仅在轻扫时向右射门,c#,android,unity3d,C#,Android,Unity3d,好的,我让它工作到球被扔到的地方,如果我向右滑动,它会朝那个方向移动,但这是它唯一的移动方向。 如果我向上滑动,或者在屏幕左侧的任何位置,球仍然会被抛向右侧。所以我的问题是,我能在我的代码中做些什么来让它朝着我正在滑动的方向滑动 using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { void Start() { } pri

好的,我让它工作到球被扔到的地方,如果我向右滑动,它会朝那个方向移动,但这是它唯一的移动方向。 如果我向上滑动,或者在屏幕左侧的任何位置,球仍然会被抛向右侧。所以我的问题是,我能在我的代码中做些什么来让它朝着我正在滑动的方向滑动

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);
    }
}
使用UnityEngine;
使用系统集合;
公共类NewBehaviourScript:MonoBehavior
{
void Start()
{
}
私有浮动长度=0;
私有布尔SW=假;
私人矢量3决赛;
私人Vector3 startpos;
私有向量3 endpos;
公共游戏对象basketball;//basketball Obj
公共刚体刚体;//篮球对象
公众浮力=0.2f;
无效更新()
{
rbody=basketball.GetComponent();
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Start)
{
最终=矢量3.0;
长度=0;
SW=假;
Vector2 touchDeltaPosition=Input.GetTouch(0.position);
startpos=新矢量3(touchDeltaPosition.x,0,touchDeltaPosition.y);
}
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Moved)
{
SW=真;
}
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Cancelled)
{
SW=假;
}
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Stational)
{
SW=假;
}
if(Input.touchCount>0&&Input.GetTouch(0.phase==TouchPhase.Ended)
{
中频(短波)
{
Vector2 touchPosition=Input.GetTouch(0.position);
endpos=新矢量3(touchPosition.x,0,touchPosition.y);
最终=结束位置-开始位置;
长度=最终大小;
rbody.AddForce(新矢量3(touchPosition.x,touchPosition.x+touchPosition.y,touchPosition.y)*Force);
}
}
}
void OnGUI()
{
GUI.Box(新的Rect(5030050030),“长度:”+长度);
}
}

触摸阶段。结束
状态,尝试更换

rbody.AddForce(新矢量3(touchPosition.x,touchPosition.x+touchPosition.y,touchPosition.y)*Force)

为此:

fin=fin.normalized;
rbody.附加力(鳍*力)


这可能会解决您的问题。

现在根本不应用强制。请尝试记录
startPos
endPos
值,以检查您是否收到正确的值。并在
AddForce()
函数下方添加
SW=false
,同时将变量名
final
更改为其他类似的
fin
。我使用GUI跟踪开始和结束位置,当我在整个屏幕上滑动时,它们都在更新。尝试将
Force
的值增加到一些
20.0f
,因为我们正在对值进行规范化,我想,增加
Force
的值仍然可以不注册屏幕的左侧。我试过rbody.AddRelativeForce(finpos*Force,ForceMode.Force);也没有成功。将final重命名为finpos。