我的 rb2d 重力比例设置为 0,但当我按下梯子上的向下箭头时,我仍然跌倒

My rb2d gravity scale is set to 0 yet I still fall when I press the down arrow on my ladder

我正在为我的 Unity 游戏制作阶梯。当您将鼠标悬停在它上面时,什么也不会发生(这很正常)。每当按下向上箭头键(或 w)时,我想通过设置 gravityscale 0 来冻结重力。值得庆幸的是,它有效。不幸的是,尽管每当我倒下时,重力仍然为 0,但不知何故我的角色又掉回了地面。

这是我的播放器代码。抱歉,格式非常糟糕;我只是删除了与梯子无关的东西,所以你不会看到无关的细节。

 // ladder
    public float climb;
    public float climbspeed;
    public float gravitystore;
    public bool laddered;
    public GameObject laddercheck;
    public float upaxis;
    public xpositionladdercheck ladderx;


    // Start is called before the first frame update
    void Start()
    {
        ladderx = FindObjectOfType<xpositionladdercheck>();
        jumptimecounter = Jumptime;
        jumpscriptbetter = FindObjectOfType<BetterJump>();
       
        gameover = false;
        respawnscreen = FindObjectOfType<RespawnIntermission>();
        lives = 5;
        atkindex = FindObjectOfType<attackindex>();
        scale =transform.localScale;
        basicattackscript = FindObjectOfType<BasicAttack>();
        basicattacksupply = 0;
        rb = GetComponent<Rigidbody2D>();
        gravitystore = rb.gravityScale;

    }

    // Update is called once per frame
    private void Update()
    {
     

        
     
        
        horimove = Input.GetAxis("Horizontal");
       
        
       
        if (isDead == false)
        {
            upaxis = Input.GetAxis("Vertical");
            anim.SetFloat("run", Mathf.Abs(horimove));
            if (Input.GetKeyDown(KeyCode.Space) && laddered)
            {
                jumpladder();
            }
            anim.SetFloat("run", Mathf.Abs(horimove));
            if (laddered && Mathf.Abs(upaxis) > .01)
            {
                rb.gravityScale = 0;
                climbm();
            } else if (!laddered)
            {
                rb.gravityScale = gravitystore;
            }
         if (laddered&&Input.GetKeyDown(KeyCode.Space))
            {
                StartCoroutine("jumpable");

            }  
           
            
        rb.velocity = new Vector2(speed * horimove * Time.fixedDeltaTime, rb.velocity.y);
            if (Input.GetKey(KeyCode.Space) && OnGround)
            {
                jump();
            }
        }


    }
    IEnumerator jumpable()
    {
      
        laddercheck.SetActive(false);
        yield return new WaitForSeconds(.8f);
        laddercheck.SetActive(true);

    }
    void climbm()
    {
        if (laddered)
        {
            if (upaxis > .1f) {
               

            } else if (!laddered)
            {
                rb.constraints = RigidbodyConstraints2D.FreezeRotation;
            }
            rb.velocity = Vector2.up * climbspeed * upaxis *Time.deltaTime;
            transform.position = new Vector2(ladderx.ladderxpos, rb.position.y);
// the ladder.x things is just a script that gets the x position of the ladder but it works well so ignore that.
            

        }
    }
    public void jump()
    {
        
        
       
           
        
   
    }public void jumpladder()
    {
        
        
       
           
        
        if (Input.GetKeyDown(KeyCode.Space))
        {

                rb.AddForce(Vector2.up * jumpforce * Time.fixedDeltaTime);
               
              

            
        }
       
    }
            
    }
   
    }

我将质量设置为最低 (.0001),但仍然发生同样的事情。我没有包括 ladderx 位置脚本或我什至在梯子脚本上的感觉,但这与问题无关。它感觉一切都很好,xposition 脚本也能正常工作。

我明白了。出于某种原因,我不得不将 Physics2D.gravity 设置为 new Vector2 (0,0);每当我爬梯子的时候。然后我制作了一个 vector 2 physgravity 变量来存储自然重力,并在我离开梯子时将其设置回原来的值。

public Vector2 physgravity;
physgravity = Physics2D.gravity;
        Physics2D.gravity = new Vector2(0f, -9.81f);
 private void Update()
    {
        if (!laddered)
        {
            Physics2D.gravity = new Vector2(0f, -9.81f);
        }
____________________ (later on in the script)
 if (laddered && Mathf.Abs(upaxis) > .01)
            {
                rb.gravityScale = 0;
                Physics2D.gravity = new Vector2(0, 0);

                rb.velocity = new Vector2(0, 0);

                climbm();