简体   繁体   English

Unity 2D Jump脚本

[英]Unity 2D Jump script

So I was trying to implement double jumping in my game, which doesn't work. 所以我试图在我的游戏中实现双跳,但这种方法不起作用。 And now, somehow, not only can't my players double jump, they can't even jump either! 而现在,不知何故,我的球员不仅不能跳双跳,甚至不能跳双跳!

update: they can jump now, still can't double jump though. 更新:他们现在可以跳了,但是仍然不能跳两次。

This is my whole movement script: 这是我的整个动作脚本:

using UnityEngine;

namespace Players
{
    public class Actor : MonoBehaviour
    {
        //in order to control both players using 1 script.
        public int playerIdx;

        //Variables.
        public float movementSpeed = 150f;
        public float jumpForce = 250f;

        //Ground stuff.
        public LayerMask whatIsGround;
        public bool grounded;

        //boolean stuff.
        private bool facingRight;
        private bool moving;

        //Needed to check if player is on the ground.
        public Transform groundCheck;

        //Limit player's movement speed.
        public float maxMovementSpeed = 400f;

        //Double jump stuff.
        private bool doubleJumpReady;

        //rb
        private Rigidbody2D rb;


        // Start is called before the first frame update
        void Start()
        {
            doubleJumpReady = true;
            rb = GetComponent<Rigidbody2D>();
            facingRight = true;
        }

        // Update is called once per frame
        void FixedUpdate()
        {
            SlowDown();
        }

        private void LateUpdate()
        {
            grounded = Physics2D.OverlapCircle(groundCheck.position, 0.1f, whatIsGround);

            if (grounded)
                doubleJumpReady = true;           

        }

        private void SlowDown()
        {

            if (moving) return;

            //if player is not moving, slow them down.
            if (rb.velocity.x > 0.2f)
                rb.AddForce(movementSpeed * Time.deltaTime * -Vector2.right);
            if (rb.velocity.x < -0.2f)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right);
        }

        public void Move(int dir)
        {
            //Flip the player.
            Flip(dir);

            //Moving the player.
            moving = true;

            float xVel = rb.velocity.x;            //Get x velocity.

            if ( dir > 0)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
            else if (dir < 0)
                rb.AddForce(movementSpeed * Time.deltaTime * Vector2.right * dir);
            else if (dir == 0) { }  //do nothing.

            //Help player turn around faster.
            if (xVel > 0.2f && dir < 0)
                rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * -Vector2.right);
            if (xVel < 0.2f && dir > 0)
                rb.AddForce(movementSpeed * 3.2f * Time.deltaTime * Vector2.right);
        }

        private void Flip(int dir)
        {
            if (facingRight && dir == -1 || !facingRight && dir == 1)
            {
                facingRight = !facingRight;
                transform.Rotate(0f, 180f, 0f);
            }
        }

        protected void Jump()
        {
            if (grounded)
            {
                rb.AddForce(Vector2.up * jumpForce);
                grounded = false;
                doubleJumpReady = true;
            }
            else if (!grounded && doubleJumpReady)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = false;
            }
        }       
    }
}


I don't know if it is because of my jump script, or my player script: 我不知道是因为我的跳转脚本还是我的播放器脚本:

void Update()
{
    if (playerIdx == 1)
    {
        if (Input.GetKey(KeyCode.A))
            Move(-1);
        if (Input.GetKey(KeyCode.D))
            Move(1);
        if (Input.GetKey(KeyCode.W))
            Jump();
    }

    if (playerIdx == 2)
    {
        if (Input.GetKey(KeyCode.LeftArrow))
            Move(-1);
        if (Input.GetKey(KeyCode.RightArrow))
            Move(1);
        if (Input.GetKey(KeyCode.UpArrow))
            Jump();
    }
}

So how can I fix this? 那么我该如何解决呢?

as far as i can see you never reset the 据我所知,您永远不会重置

doubleJumpReady = false;

Variable. 变量。 To fix this simply change the jump code to: 要解决此问题,只需将跳转代码更改为:

protected void Jump()
{
    if (grounded)
    {
        rb.AddForce(Vector2.up * jumpForce);
        grounded = false;
        doubleJumpReady = true;
    }
    else if (!grounded && doubleJumpReady)
    {
        rb.AddForce(Vector2.up * jumpForce);
        doubleJumpReady = false;
    }
}

Hope it works ;). 希望它能工作;)。

EDIT: grounded is set by overlapping spheres. 编辑:接地是由重叠的球体设置的。 Therefore no need to set it here. 因此,无需在此处进行设置。 Use this code and press your jump btn 2 times and see if the Debug.Log message shows up. 使用此代码并按两次btn跳转,然后查看Debug.Log消息是否出现。 Also, your player ID (idx is not needed.) As far as i can see your script is attached two to different objects. 另外,您的播放器ID(不需要idx。)据我所知,您的脚本已将两个附加到不同的对象。 Therefore their variables are not shared anyways. 因此,无论如何都不会共享它们的变量。

        protected void Jump()
        {
            if (grounded)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = true;
            }
            else if (!grounded && doubleJumpReady)
            {
                rb.AddForce(Vector2.up * jumpForce);
                doubleJumpReady = false;
                Debug.Log("I am double jumping");

            }
        } 

And the final problem is, you do not execute one of your jumps you execute both at once. 最后一个问题是,您不执行一次跳跃,一次执行两次。 THis happens due to your execution. 这是由于您的执行而发生的。

Input.GetKey(KeyCode.UP)

instead use: 改为使用:

Input.GetKeyDown(KeyCode.Up);

GetKeyDown returns true when the button is pressed. 按下按钮时,GetKeyDown返回true。 GetKey returns true WHILE the button is pressed. 当按下按钮时,GetKey返回true。

Hope it works now ;) 希望它现在可以工作;)

I would implement it with a counter, you can set the number of jumps you want. 我将使用计数器来实现它,您可以设置所需的跳转次数。

The code would be like this: 代码如下:

jumpCount = 0;

protected void Jump()
{
    if(!grounded && jumpCount < 2)
    {
        jumpCount++;
        rb.AddForce(Vector2.up * jumpForce);
    }
    if(grounded)
        jumpCount = 0;
}

Going off the assumption that you can now perform the normal jump again after reading the comments. 不再假设您现在可以在阅读注释后再次执行正常跳转。 I think the reason you can't 'double jump' is that when you call the Jump() method, you don't just call it once, you call it twice, so what happens is the player jumps and then immediately double jumps and so you don't actually notice that the double jump has occurred. 我认为您不能“两次跳转”的原因是,当您调用Jump()方法时,您不仅会调用一次,而且会调用两次,所以发生的情况是玩家跳了起来,然后立即跳了两次,并且因此您实际上并没有注意到发生了两次跳跃。 You could make it so that your doubleJumpReady boolean is only true after a set amount of time after you have jumped initially using some sort of co-routine or something I implemented for a sort of double jump mechanic once was that the user could press the jump button again to double jump only when the player had reached the maximum height of the initial jump or after. 您可以这样做,使得doubleJumpReady布尔值仅在您最初使用某种协例程或我为某种双跳转机制实现的某种东西(用户可以按下跳转)实现跳转之后的一段时间后才为true仅当播放器达到或超过初始跳跃的最大高度时,再按一次按钮即可进行两次跳跃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM