【问题标题】:Unity 2D Jump scriptUnity 2D 跳转脚本
【发布时间】:2019-05-22 05:11:16
【问题描述】:

所以我试图在我的游戏中实现二段跳,但这是行不通的。而现在,不知何故,我的球员不仅不能二段跳,甚至连跳也不能!

更新:他们现在可以跳,但仍然不能二段跳。

这是我的整个动作脚本:

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


不知道是因为我的跳转脚本,还是我的播放器脚本:

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();
    }
}

那么我该如何解决这个问题?

【问题讨论】:

  • 按跳转键有没有报错? grounded 是否曾经设置为 true?代码是否到达rb.AddForce?如果没有更多信息,将很难解决这个问题。
  • 按跳转没有错误;这应该将 grounded 设置为 true: grounded = Physics2D.OverlapCircle(groundCheck.position, 0.7f, whatIsGround);
  • rb.AddForce(Vector2.up * jumpForce); 替换为rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); 会发生什么
  • 好的,解决了跳跃问题,谢谢。但是你知道我该怎么做二段跳吗?
  • 你到底在哪里设置 doublejumpready 为真?

标签: c# unity3d


【解决方案1】:

据我所知,您永远不会重置

doubleJumpReady = false;

变量。要解决此问题,只需将跳转代码更改为:

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

希望它有效;)。

编辑: 接地由重叠的球体设置。因此无需在此处设置。 使用此代码并按两次跳转 btn 并查看 Debug.Log 消息是否显示。此外,您的玩家 ID(不需要 idx。)据我所知,您的脚本将两个附加到不同的对象。因此,它们的变量无论如何都不会共享。

        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");

            }
        } 

最后一个问题是,你不会同时执行一个跳转。 这是由于您的执行而发生的。

Input.GetKey(KeyCode.UP)

改为使用:

Input.GetKeyDown(KeyCode.Up);

GetKeyDown 在按下按钮时返回 true。 GetKey 在按下按钮时返回 true。

希望它现在可以工作;)

【讨论】:

  • 好吧,它真的不起作用:(。一个玩家(因为我的游戏中有两个玩家)像往常一样跳跃,当我按住跳跃按钮时,另一个只是飞向天空。
  • 我认为这是因为我的第二个玩家的“接地”布尔值出现了故障或其他问题。当它跳跃时,接地仍然保持真实,使其能够飞行。但是我把它们都设置成一样了,那是怎么发生的呢?
  • 如果您的 isGrounded Check 基于碰撞,请在检查器中检查它们的设置。此外,接地变量是在哪里创建的?你能发布完整的移动脚本吗?
  • 我知道出了什么问题。我拖错了地面检查。虽然还是不能二段跳
  • 我按了一次跳转,debug.log 出现了,所以我认为它会自动跳转?
【解决方案2】:

我会用一个计数器来实现它,你可以设置你想要的跳跃次数。

代码是这样的:

jumpCount = 0;

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

【讨论】:

  • 好吧,他们不能再跳了:(
  • 如果接地布尔值按预期工作,它应该可以工作。
  • 我将接地布尔值公开以在检查器中看到它,一个玩家工作,一个不工作?
【解决方案3】:

假设您现在可以在阅读 cmets 后再次执行正常跳转。我认为你不能'双跳'的原因是当你调用Jump()方法时,你不只是调用它一次,你调用它两次,所以发生的是玩家跳跃然后立即双跳然后所以你实际上并没有注意到已经发生了二段跳。您可以做到这一点,以便您的 doubleJumpReady 布尔值仅在您最初使用某种协同程序跳跃后的一段时间后才为真,或者我曾经为一种双跳机制实现的东西是用户可以按下的只有当玩家达到初始跳跃的最大高度或之后,才能再次跳跃按钮进行二段跳。

【讨论】:

  • 感谢 franz 的回答,它现在可以工作了。无论如何谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多