一旦能量被选择,就试着发射大炮

本文关键字:发射 能量 选择 | 更新日期: 2023-09-27 17:50:28

基本上我已经得到了一个项目,在unity中使用c#和设备Leap Motion开发一款游戏。我遇到的问题是,我有一个基本的能量条,它在0 - 100的范围内上下移动,当用户松开拳头时,我想使用选择的能量射击。我遇到的问题是,我无法想出一种方法来设置bool "shoot"为真释放拳头。


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

我希望这段代码对你有意义。所发生的一切是,如果你把拳头放在Leap传感器上方,球的力量会增加,直到你打到100,然后它会下降到0,以此类推,直到你松开拳头。我需要一种方法来将"射击"bool设置为true,一旦一个权力被选中,就像我目前做的那样,我只能在权力选择期间将其设置为true,或者在你握紧拳头之前将其设置为true。

一旦能量被选择,就试着发射大炮

一个比较简单的方法是设置一个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
    }
}

应该可以工作,除非我误解了代码/需求。