简体   繁体   中英

Stuck to Wall in Unity2D?

I'm making a basic platformer game and this is just the basics, when I noticed that if I press a movement key up against a wall, I stick to it. Is there a way to fix this? Is it a problem with my code, the settings in the Unity Editor, or both?

Here's the code (I'm a new developer so it's probably really bad):

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    //config params
    [SerializeField] public float moveSpeed;
    [SerializeField] public float jumpForce;
    [SerializeField] private bool usingJoystick;
    [SerializeField] private int maxJumps;
    [SerializeField] private float xDrag;
    
    //control vars
    private float moveInput;
    private int jumpsAvailable;

    //components
    private Rigidbody2D myRigBody;

    void Start()
    {
        myRigBody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        if (usingJoystick)
        {
            moveInput = Input.GetAxis("Horizontal");
            myRigBody.velocity = new Vector2(moveInput * moveSpeed, myRigBody.velocity.y);
        }
        else
        {
            HorMoveKeyDown();
            JumpKeyDown();
        }
        HorDrag();
    }

    private void HorDrag()
    {
        if (!(Input.GetKey("a") || Input.GetKey("d")))
        {
            if (myRigBody.velocity.x >= -0.1f && myRigBody.velocity.x <= 0.1f)
            {
                myRigBody.velocity = new Vector2(0f, myRigBody.velocity.y);
            }
            if (myRigBody.velocity.x < 0)
            {
                myRigBody.velocity = new Vector2(myRigBody.velocity.x + xDrag, myRigBody.velocity.y);
            }
            else if (myRigBody.velocity.x > 0)
            {
                myRigBody.velocity = new Vector2(myRigBody.velocity.x - xDrag, myRigBody.velocity.y);
            }
        }
    }

    private void JumpKeyDown()
    {
        if (Input.GetKeyDown("space"))
        {
            if (jumpsAvailable > 0)
            {
                myRigBody.velocity = new Vector2(myRigBody.velocity.x, jumpForce);
            }
        }
    }

    private void HorMoveKeyDown()
    {
        if (Input.GetKey("a"))
        {
            myRigBody.velocity = new Vector2(-moveSpeed, myRigBody.velocity.y);
        }
        else if (Input.GetKey("d"))
        {
            myRigBody.velocity = new Vector2(moveSpeed, myRigBody.velocity.y);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        jumpsAvailable = maxJumps;
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        jumpsAvailable--;
    }
}

This is because you have too much friction on the walls with your player.

Create physics material in your assets folder. Set the friction to 0 so it won't have any friction. Then click on your player in the hierarchy and then you have your (box)collider2D in the inspector. Drag the physics material to the 'Material' on the (box)collider2D on the player.

You do not need to add this physics material to your wall. Only to your player.

Make sure to read the documentation of physics materials. https://docs.unity3d.com/Manual/class-PhysicMaterial.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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