C# Unity3D球未在刷球时抛出

C# Unity3D球未在刷球时抛出,c#,android,unity3d,C#,Android,Unity3d,我正试着用侧击检测扔篮球。 根据击球的长度,将确定施加的力度(投球的距离)。GUI会更新,因此会检测到我的击球,但篮球根本不会移动。我试过检查运动学和取消检查,结果都是一样的。 脚本附在我的相机上。我将ball对象拖动到从公共变量创建的对象和刚体位置。 这是我目前正在使用的代码 using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { void Star

我正试着用侧击检测扔篮球。 根据击球的长度,将确定施加的力度(投球的距离)。GUI会更新,因此会检测到我的击球,但篮球根本不会移动。我试过检查运动学和取消检查,结果都是一样的。 脚本附在我的相机上。我将ball对象拖动到从公共变量创建的对象和刚体位置。 这是我目前正在使用的代码

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);
    }
}
使用UnityEngine;
使用系统集合;
公共类NewBehaviourScript:MonoBehavior
{
void Start()
{
}
私有浮动长度=0;
私有布尔SW=假;
私人矢量3决赛;
私人Vector3 startpos;
私有向量3 endpos;
公共游戏对象basketball;//basketball Obj
公共刚体刚体;//篮球对象
无效更新()
{
rbody=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,0,touchPosition.y));
}
}
}
void OnGUI()
{
GUI.Box(新的Rect(5030050030),“长度:”+长度);
}
}

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

请尝试此代码,并确保basketball已附加刚体组件

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));
            }
        }
    }
void Update()
{
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));
}
}
}

您根本没有在AddForce中使用长度变量!这是我第一次使用AddForce,正确的语法是什么@语法正确!我的意思是,你正在计算“长度”的值,但没有在任何地方使用它!也许可以试试rbody.AddForce(新矢量3(touchPosition.x,0,touchPosition.y)*长度);仍然没有做任何事情rbody=GetComponent();这条线取篮球对象rb?如果这个代码被附加到相机上,它不会尝试拍摄相机的rb吗?好的,这是可行的,sorta。现在球消失了。我想它发射得太快了。我正在使用rbody.AddForce(新的Vector3(touchPosition.x,0,touchPosition.y));而不是把它乘以。尝试rbody.AddForce(新矢量3(touchPosition.x,0,touchPosition.y)*100);同样的事情也发生了。试着乘以0.1f的系数,然后向上移动,你也可以增加一个公共浮力;在你的类中,并从编辑器中更改它以方便使用,即使不乘以它,球也会消失,所以这不会仍然导致相同的结果吗?乘以因子0.1f将只使用新矢量3的10%,因此在没有因子的情况下,它实际上会将施加的力减少90%。