C# 选择电源后尝试发射加农炮

C# 选择电源后尝试发射加农炮,c#,unity3d,leap-motion,C#,Unity3d,Leap Motion,基本上,我得到了一个项目,用c#和设备Leap Motion在unity中开发一个游戏。我遇到的问题是,我有一个基本的功率条,它在0-100范围内上下移动,当用户释放拳头时,我想使用选定的功率进行射击。我遇到的问题是,我想不出一种方法,在松开拳头时,将枪声“射击”设置为真 if(frame.Hands.Count>=2) { Hand leftHand=GetLeftMostHand(框架); Hand rightHand=GetRightMostHand(帧); Vector3 handD

基本上,我得到了一个项目,用c#和设备Leap Motion在unity中开发一个游戏。我遇到的问题是,我有一个基本的功率条,它在0-100范围内上下移动,当用户释放拳头时,我想使用选定的功率进行射击。我遇到的问题是,我想不出一种方法,在松开拳头时,将枪声“射击”设置为真



if(frame.Hands.Count>=2)
{
Hand leftHand=GetLeftMostHand(框架);
Hand rightHand=GetRightMostHand(帧);
Vector3 handDiff=leftHand.PalmPosition.ToUnityScaled()-rightHand.PalmPosition.ToUnityScaled();
Vector3 newRot=CannonBarrel.transform.localRotation.eulerAngles;
浮动左右速度=手差y*5.0f;
浮动手距=左手方向节距+右手方向节距*0.5f;
newRot.x=0;
newRot.y=90;
纽洛特z=70+-手距*20.0f;
射击=真;
//如果握紧拳头。。。
if(frame.Fingers.Count<3)
{
leftRightSpeed=0;
射击=假;
如果(增加==真)
{
ballPower++;
如果(球功率>=100)
{
增加=错误;
}
}
else if(递增==false)
{
球力--;

如果(ballPower一种比较简单的方法是设置一个bool来检查玩家是否至少握拳一次,从而设置力量。我们称之为
hasClenchedFist

if (frame.Hands.Count >= 2)
    {
    Hand leftHand = GetLeftMostHand(frame);
    Hand rightHand = GetRightMostHand(frame);

    Vector3 handDiff = leftHand.PalmPosition.ToUnityScaled() - rightHand.PalmPosition.ToUnityScaled();
    Vector3 newRot = CannonBarrel.transform.localRotation.eulerAngles;
    float leftRightSpeed = handDiff.y * 5.0f;
    float handPitch = leftHand.Direction.Pitch + rightHand.Direction.Pitch * 0.5f; 

    newRot.x = 0;
    newRot.y = 90;
    newRot.z = 70 + -handPitch * 20.0f;

    //shoot = true;

    // if closed fist...
    if (frame.Fingers.Count < 3)
    {
        leftRightSpeed = 0;
        hasClenchedFist = true;
        shoot = false;

        if (increasing == true)
        {
            ballPower++;

            if (ballPower >= 100)
            {
                increasing = false;
            }
        }
        else if (increasing == false)
        {
            ballPower--;

            if (ballPower <= 0)
            {
                increasing = true;
            }
        }
    }
    else if(hasClenchedFist)
    {
        shoot = true;
    }
    else 
    {
        //Move left or right depending on hands height difference.
        transform.parent.rigidbody.velocity = transform.parent.right * leftRightSpeed;
        //Rotate the barrel
        CannonBarrel.transform.localRotation = Quaternion.Slerp(CannonBarrel.transform.localRotation, Quaternion.Euler(newRot), 0.1f);
    }

    if (shoot == true)
    {
        Debug.Log("fired");
        //add code here to spawn projectile
    }

}
if(frame.Hands.Count>=2)
{
Hand leftHand=GetLeftMostHand(框架);
Hand rightHand=GetRightMostHand(帧);
Vector3 handDiff=leftHand.PalmPosition.ToUnityScaled()-rightHand.PalmPosition.ToUnityScaled();
Vector3 newRot=CannonBarrel.transform.localRotation.eulerAngles;
浮动左右速度=手差y*5.0f;
浮动手距=左手方向节距+右手方向节距*0.5f;
newRot.x=0;
newRot.y=90;
纽洛特z=70+-手距*20.0f;
//射击=真;
//如果握紧拳头。。。
if(frame.Fingers.Count<3)
{
leftRightSpeed=0;
hasClenchedFist=true;
射击=假;
如果(增加==真)
{
ballPower++;
如果(球功率>=100)
{
增加=错误;
}
}
else if(递增==false)
{
球力--;

如果(ballPower)谢谢你的回答,你的回答很有帮助。可惜Leap Motion sensor不能很好地确定两个握紧的拳头和两个平手掌之间的区别,我仍然会遇到这样的问题:它认为我需要在不应该的时候射击。
if (frame.Hands.Count >= 2)
    {
        Hand leftHand = GetLeftMostHand(frame);
        Hand rightHand = GetRightMostHand(frame);

        Vector3 handDiff = leftHand.PalmPosition.ToUnityScaled() - rightHand.PalmPosition.ToUnityScaled();
        Vector3 newRot = CannonBarrel.transform.localRotation.eulerAngles;
        float leftRightSpeed = handDiff.y * 5.0f;
        float handPitch = leftHand.Direction.Pitch + rightHand.Direction.Pitch * 0.5f; 

        newRot.x = 0;
        newRot.y = 90;
        newRot.z = 70 + -handPitch * 20.0f;

        shoot = true;

        // if closed fist...
        if (frame.Fingers.Count < 3)
        {
            leftRightSpeed = 0;
            shoot = false;

            if (increasing == true)
            {
                ballPower++;

                if (ballPower >= 100)
                {
                    increasing = false;
                }
            }
            else if (increasing == false)
            {
                ballPower--;

                if (ballPower <= 0)
                {
                    increasing = true;
                }
            }
        }

        else 
        {
            //Move left or right depending on hands height difference.
            transform.parent.rigidbody.velocity = transform.parent.right * leftRightSpeed;
            //Rotate the barrel
            CannonBarrel.transform.localRotation = Quaternion.Slerp(CannonBarrel.transform.localRotation, Quaternion.Euler(newRot), 0.1f);
        }

        if (shoot == true)
        {
            Debug.Log("fired");
            //add code here to spawn projectile
        }

    }
if (frame.Hands.Count >= 2)
    {
    Hand leftHand = GetLeftMostHand(frame);
    Hand rightHand = GetRightMostHand(frame);

    Vector3 handDiff = leftHand.PalmPosition.ToUnityScaled() - rightHand.PalmPosition.ToUnityScaled();
    Vector3 newRot = CannonBarrel.transform.localRotation.eulerAngles;
    float leftRightSpeed = handDiff.y * 5.0f;
    float handPitch = leftHand.Direction.Pitch + rightHand.Direction.Pitch * 0.5f; 

    newRot.x = 0;
    newRot.y = 90;
    newRot.z = 70 + -handPitch * 20.0f;

    //shoot = true;

    // if closed fist...
    if (frame.Fingers.Count < 3)
    {
        leftRightSpeed = 0;
        hasClenchedFist = true;
        shoot = false;

        if (increasing == true)
        {
            ballPower++;

            if (ballPower >= 100)
            {
                increasing = false;
            }
        }
        else if (increasing == false)
        {
            ballPower--;

            if (ballPower <= 0)
            {
                increasing = true;
            }
        }
    }
    else if(hasClenchedFist)
    {
        shoot = true;
    }
    else 
    {
        //Move left or right depending on hands height difference.
        transform.parent.rigidbody.velocity = transform.parent.right * leftRightSpeed;
        //Rotate the barrel
        CannonBarrel.transform.localRotation = Quaternion.Slerp(CannonBarrel.transform.localRotation, Quaternion.Euler(newRot), 0.1f);
    }

    if (shoot == true)
    {
        Debug.Log("fired");
        //add code here to spawn projectile
    }

}