Unity3D Ball不会被轻扫

时间:2017-07-03 09:38:36

标签: c# android unity3d

我试图使用滑动检测来投篮。 根据滑动的长度将决定施加的力量(投掷距离球。)GUI更新,因此我的滑动被检测到但篮球根本不移动。我尝试检查运动学并取消检查,结果相同。 该脚本已附加到我的相机。我将球对象拖动到从公共变量创建的对象和刚体位置。 这是我目前正在使用的代码;

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);
    }
}

1 个答案:

答案 0 :(得分:0)

这可能是问题, rbody 引用了相机的刚体(如果有的话)

尝试此代码,并确保 篮球 附加了RigidBody组件!

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));
            }
        }
    }