Question

Basically I have been given a project to develop a game in unity using c# and the device Leap Motion. The problem I am having is that I have a basic power bar that goes up and down through a range of 0 - 100, when the user releases his fist I want to shoot using the selected power. the problem I am having is I cannot figure out a way to set the bool "shoot" to true on release of the fists.


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
        }

    }

I hope that this code makes sense to you. All that is happening is if you're holding your fists above the Leap sensor the ball power will increase till you hit 100, then it will decrease back down to 0 and so on until you release your fists. I need a way to set the "shoot" bool to true once a power has been selected as the way I currently do it I can only set it to true either during the power selection or it gets set to true before you clench your fists.

Thanks in advance.

Était-ce utile?

La solution

A somewhat simple way you could do this is by setting a bool to check if the player has clenched his fist at least once, thus setting the power. Let's call it 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
    }

}

This should work, unless I've misunderstood the code/requirements.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top