Unity3d 我怎样才能改变代码,使我的操纵杆只让我的角色改变跳跃速度一次?

Unity3d 我怎样才能改变代码,使我的操纵杆只让我的角色改变跳跃速度一次?,unity3d,Unity3d,我设置了一个操纵杆来让我的角色四处移动 public Joystick joystick; private void Update() { if (joystick.Horizontal >= .2f) { horizontalMove = runSpeed; } else if (joystick.Horizontal <= -0.2f) {

我设置了一个操纵杆来让我的角色四处移动

public Joystick joystick;

 private void Update()
    {
        if (joystick.Horizontal >= .2f)
        {
            horizontalMove = runSpeed;
        }
        else if (joystick.Horizontal <= -0.2f)
        {
            horizontalMove = -runSpeed;
        }
        else
        {
            horizontalMove = 0;
        }

        float verticalMove = joystick.Vertical;

        if (verticalMove >= .8f && controller.m_Grounded)
        {
            jump = true;
        }
        else
        {
            jump = false;
        }
    }
我的角色控制器脚本如下所示

public void Move(float move, bool crouch, bool jump)
    {
        if (knockbackCount <= 0)
        {
            // Move the character by finding the target velocity
            Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
            // And then smoothing it out and applying it to the character
            m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref velocity, m_MovementSmoothing);
            // If the input is moving the player right and the player is facing left...
            if (move > 0 && !m_FacingRight)
            {
                // ... flip the player.
                Flip();
            }
            // Otherwise if the input is moving the player left and the player is facing right...
            else if (move < 0 && m_FacingRight)
            {
                // ... flip the player.
                Flip();
            }
            // If the player should jump...
            if (m_Grounded == true && jump)
            {
                // Add a vertical force to the player.
                m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
                animator.SetTrigger("takeOff");
            }

            if (m_Grounded && move != 0)
            {
                m_CapsuleCollider.sharedMaterial = m_Slippery;
            }
            else
            {
                m_CapsuleCollider.sharedMaterial = m_Rigid;
            }
            if (!m_Grounded)
            {
                m_CapsuleCollider.sharedMaterial = m_Slippery;
                animator.SetBool("isJumping", true);
            }
            else
            {
                animator.SetBool("isJumping", false);
            }
        }
        if (knockFromRight && knockbackCount > 0)
        {
            m_Rigidbody2D.velocity = new Vector2(-knockback, knockback / 2);
        }
        if (!knockFromRight && knockbackCount > 0)
        {
            m_Rigidbody2D.velocity = new Vector2(knockback, knockback / 2);
        }
        knockbackCount = -1;
    }
public void Move(漂浮移动、俯卧撑、俯跳)
{
if(击退计数0&!m_FacingRight)
{
//…翻转播放器。
翻转();
}
//否则,如果输入向左移动播放器,且播放器面向右侧。。。
否则如果(移动<0&&m_朝向右侧)
{
//…翻转播放器。
翻转();
}
//如果玩家应该跳。。。
如果(m_Grounded==true&&jump)
{
//向玩家添加垂直力。
m_Rigidbody2D.AddForce(新矢量2(0f,m_JumpForce));
设置触发器(“起飞”);
}
如果(m_接地和移动!=0)
{
m_CapsuleCollider.sharedMaterial=m_slided;
}
其他的
{
m_CapsuleCollider.sharedMaterial=m_刚性;
}
如果(!m_接地)
{
m_CapsuleCollider.sharedMaterial=m_slided;
SetBool(“isJumping”,true);
}
其他的
{
动画师.SetBool(“isJumping”,false);
}
}
if(knockFromRight&&knockbackCount>0)
{
m_rigiidbody2d.velocity=新向量2(-knockback,knockback/2);
}
如果(!knockFromRight&&knockbackCount>0)
{
m_rigiidbody2d.velocity=新向量2(击退,击退/2);
}
敲回计数=-1;
}

我做错了什么?我尝试将所有内容移动到一个脚本中,以及删除“if m_Grounded==true&&jump”行,并在操纵杆行下添加力。

您可以添加一个额外的
bool canJump
,当您设置
jump=true
时,它被设置为
false
。然后,仅当操纵杆低于特定阈值时才重新启用(
verticalMove此操作非常有效!谢谢!您可以添加一个附加的
bool canJump
,当您设置
jump=true
时,它被设置为
false
。然后,仅当操纵杆低于特定阈值时才重新启用(
verticalMove此操作非常有效!谢谢!
public void Move(float move, bool crouch, bool jump)
    {
        if (knockbackCount <= 0)
        {
            // Move the character by finding the target velocity
            Vector3 targetVelocity = new Vector2(move * 10f, m_Rigidbody2D.velocity.y);
            // And then smoothing it out and applying it to the character
            m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref velocity, m_MovementSmoothing);
            // If the input is moving the player right and the player is facing left...
            if (move > 0 && !m_FacingRight)
            {
                // ... flip the player.
                Flip();
            }
            // Otherwise if the input is moving the player left and the player is facing right...
            else if (move < 0 && m_FacingRight)
            {
                // ... flip the player.
                Flip();
            }
            // If the player should jump...
            if (m_Grounded == true && jump)
            {
                // Add a vertical force to the player.
                m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
                animator.SetTrigger("takeOff");
            }

            if (m_Grounded && move != 0)
            {
                m_CapsuleCollider.sharedMaterial = m_Slippery;
            }
            else
            {
                m_CapsuleCollider.sharedMaterial = m_Rigid;
            }
            if (!m_Grounded)
            {
                m_CapsuleCollider.sharedMaterial = m_Slippery;
                animator.SetBool("isJumping", true);
            }
            else
            {
                animator.SetBool("isJumping", false);
            }
        }
        if (knockFromRight && knockbackCount > 0)
        {
            m_Rigidbody2D.velocity = new Vector2(-knockback, knockback / 2);
        }
        if (!knockFromRight && knockbackCount > 0)
        {
            m_Rigidbody2D.velocity = new Vector2(knockback, knockback / 2);
        }
        knockbackCount = -1;
    }