【问题标题】:Problems with friction ( Physic2D) in Unity3DUnity3D中的摩擦问题(Physic2D)
【发布时间】:2022-08-23 21:10:52
【问题描述】:

描述问题:当我按下键盘上的 D 键时,播放器移动到右侧。我按空格键跳跃,然后按住 D 键,让我的玩家粘在平台的右侧。我怎样才能阻止这个? (使玩家跌落)。
图解视频:google drive link

剧本: 使用 UnityEngine; 使用 UnityEngine.InputSystem; 公共类 PlayerMovement : MonoBehaviour {

    Vector2 vector2Move;
    Rigidbody2D rb2D;
    [SerializeField] float climbSpeed;
    [SerializeField] float playerSpeed;
    Animator animatorPlayer;
    [SerializeField] float jumpForce;
    bool isGrounded;
    Collider2D playerCollider;
    float theCurrentGravityScale;
    private void Awake()
    {
        rb2D = GetComponent<Rigidbody2D>();
        animatorPlayer = GetComponent<Animator>();
        playerCollider = GetComponent<Collider2D>();
    }
    void Start()
    {
        theCurrentGravityScale = rb2D.gravityScale;  
        
    }
    void Update()
    {
        Run();
        FlipSprite();
        ClimbLadder();
        CheckingIfPlayerIsJumping();
    }

    private void CheckingIfPlayerIsJumping()
    {
        if(rb2D.velocity.y > 0.0001)
        {
            animatorPlayer.SetBool(\"isGrounded\", false);
            animatorPlayer.SetBool(\"isFalling\", false);
        }
         if(rb2D.velocity.y < -0.000001)
        {
            animatorPlayer.SetBool(\"isFalling\", true ); 
        }
        else
        {
            animatorPlayer.SetBool(\"isFalling\", false);
        }
       
    }

    private void FlipSprite()
    {
        bool ifPlayerismoving = Mathf.Abs(rb2D.velocity.x) > 0;
        if (ifPlayerismoving) {
            if (rb2D.velocity.y > 0.0001) {
                animatorPlayer.SetBool(\"isRunning\", false);
            }
            
                transform.localScale = new Vector2(Mathf.Sign(vector2Move.x), 1);
            
        }
        else
        {
            animatorPlayer.SetBool(\"isRunning\", false);
        }
    }

    void OnJump(InputValue value)
    {
        if (playerCollider.IsTouchingLayers(LayerMask.GetMask(\"Ground\")) && value.isPressed)
        {
            rb2D.velocity += new Vector2(0f, jumpForce);
            animatorPlayer.SetBool(\"isIdling\", false);
        }  
    }
    void OnMove(InputValue value)
    {
            vector2Move = value.Get<Vector2>();
    }

    void Run()
    {
            animatorPlayer.SetBool(\"isRunning\", true);

            Vector2 playerMovement = new Vector2(vector2Move.x * playerSpeed, rb2D.velocity.y);
            rb2D.velocity = playerMovement;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        animatorPlayer.SetBool(\"isGrounded\", true);  
    }
    void ClimbLadder()
    {
        if (playerCollider.IsTouchingLayers(LayerMask.GetMask(\"Ladder\")))
        {
           
            if (rb2D.velocity.y < 0.0001 && rb2D.velocity.y > -0.0001)
            {
                animatorPlayer.SetBool(\"isClimbing\",false);
                animatorPlayer.SetBool(\"isIdling\", true);
            }
            else
            {
                animatorPlayer.SetBool(\"isIdling\", false);
                animatorPlayer.SetBool(\"isClimbing\", true);
            }
            rb2D.gravityScale = 0;
            Vector2 climbingLadder = new Vector2(rb2D.velocity.x, vector2Move.y * climbSpeed);
            rb2D.velocity = climbingLadder;

        }
        else
        {
            animatorPlayer.SetBool(\"isClimbing\", false);
            rb2D.gravityScale = theCurrentGravityScale;
        }
    }

    标签: c# unity3d


    【解决方案1】:

    在我看来,玩家对撞机被瓷砖地图对撞机卡住了,您可以创建一个新的物理材质 2D(Assets > Create > 2D > Physics Material 2D)并将摩擦力设置为 0

    那么你需要将材质分配给玩家的刚体,玩家身上有多少碰撞体?

    【讨论】:

    • 只有一个:>>>>>
    • 您可能需要稍后添加第二个对撞机以避免双跳,物理材料是否有效? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多