一旦选择了电源,就试图发射大炮

时间:2014-03-16 20:50:21

标签: c# unity3d leap-motion

基本上我已经获得了一个使用c#和Leap Motion设备开发统一游戏的项目。我遇到的问题是我有一个基本的电源条,在0到100的范围内上下移动,当用户释放拳头我想用选定的电源拍摄时。我遇到的问题是我无法找到一种方法来设置bool"拍摄"在发布拳头时确实如此。


Controller m_leapController;
public GameObject CannonBarrel;
[Range(0, 100)] public float ballPower = 0f;
public bool increasing = true;
public bool shoot = false;

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
        }

    }

我希望这段代码对你有意义。所有发生的事情都是如果你将拳头放在闰传感器上方,球的力量会增加直到你达到100,然后它会降低到0,依此类推,直到你释放拳头为止。我需要一种方法来设置&#34;拍摄&#34; bool to true一旦选择了一个电源作为我目前的方式,我只能在电源选择期间将其设置为true,或者在你握紧拳头之前将其设置为true。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以做一个简单的方法就是设置一个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
    }

}

这应该有效,除非我误解了代码/要求。