Player gets stuck on platform | Unity2D

I am working on a infinite runner game for android using Unity. The player runs (automatically) and has to avoid the obstacles by touching the screen and changing the gravity. But i sometimes when he hits the corner/side of the platform, the player gets stuck. I already applied a “noFriction” material on the player but it still happens sometimes. (The small boxcollider under the player just checks if the player is touching the ground)

Here are some pictures:

This will either be because of the colliders you’re not showing on the platform or your movement control code. Friction won’t keep it attached like that unless your gravity is extremely low.

Thanks for your reply. Here is my Movement Code:

public class Movement : MonoBehaviour
{   
    public float moveSpeed = 5f;
    public float acceleration;
    public float jumpVelocity = 1f;
    public bool isGrounded = false;
    public Rigidbody2D rb;
    public Transform spawnpoint;
    public bool dead = false;
    public GameObject deathScreen;
    public TextMeshProUGUI speedText;



    // Start is called before the first frame update
    void Start()
    {
        rb.freezeRotation = true;
        rb.gravityScale = jumpVelocity;

    }

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

        Vector3 movement = new Vector3(2f, 0f, 0f);
        transform.position += movement * Time.deltaTime * moveSpeed;
       
       
        speedText.text = moveSpeed.ToString();
        Jump();
        CheckSpeed();   

    }
    void FixedUpdate(){
        if(moveSpeed <= 18){
            moveSpeed = moveSpeed * acceleration;
        }else{
            moveSpeed = moveSpeed * 1;
        }
    }
    void Jump() {
       
        if((Input.GetKeyDown("space") || Input.GetMouseButtonDown(0))  && isGrounded == true){
           
            rb.gravityScale *= -1;
           
            if(rb.gravityScale <= 0){
                gameObject.transform.rotation = Quaternion.Euler(180, 0, 0);

            }else{
                gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);           
            }
        }
    }
    private void CheckSpeed(){
        if(moveSpeed <= 10){
            if(rb.gravityScale<0){
            jumpVelocity = -10f;
            rb.gravityScale = jumpVelocity;

            }else{
            jumpVelocity = 10f;
                    rb.gravityScale = jumpVelocity;

            }
        }else if(moveSpeed > 10 && moveSpeed <= 12){
            if(rb.gravityScale<0){
                jumpVelocity = -15f;
                rb.gravityScale = jumpVelocity;

            }else{
                jumpVelocity = 15f;
                rb.gravityScale = jumpVelocity;

            }               
        }else if(moveSpeed > 12 && moveSpeed <= 15){
            if(rb.gravityScale<0){
                jumpVelocity = -20f;
                rb.gravityScale = jumpVelocity;

            }else{
                jumpVelocity = 20f;
                rb.gravityScale = jumpVelocity;

            }       
        }else{
            if(rb.gravityScale<0){
                jumpVelocity = -25f;
                rb.gravityScale = jumpVelocity;

            }else{
                jumpVelocity = 25f;
                rb.gravityScale = jumpVelocity;

            }
        }
           
           
    }
    private void OnTriggerEnter2D(Collider2D other){
           
            if(other.gameObject.CompareTag("Kill")){
               
                gameObject.transform.position = spawnpoint.position;
                gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);   
                dead = true;
                rb.gravityScale = 100;
                moveSpeed = 0;
                deathScreen.gameObject.SetActive(true);
            }
               
    }
}

and this is what the Inspector of the platforms look like:

6187014--678213--4.PNG

The whole point of the Rigidbody2D is to control the Transform and move via its velocity yet you are explicitly overriding physics and modifying the Transform yourself per-frame, even though physics isn’t running per-frame.

Do not modify the Transform when using physics. Use the Rigidbody2D API for movement.

Ohh okay, sorry i am new to unity and i followed a couple of tutorials for the movement. I tried it with velocity but i think i am doing it wrong. I made a new method which sets rb.velocity = movement * movespeed. But it doesnt seem to accelerate and the gravity is very weak.

 void LateUpdate()
    {

        movement = new Vector3(2f, 0f, 0f);
        //transform.position += movement * Time.deltaTime * moveSpeed;
       
        speedText.text = moveSpeed.ToString();
        Jump();
        CheckSpeed();   

    }
    void FixedUpdate(){
        moveChar(movement);
           

        if(moveSpeed <= 18){
            moveSpeed = moveSpeed * acceleration;
        }else{
            moveSpeed = moveSpeed * 1;
        }
    }
   
    void moveChar(Vector3 movement){
        rb.velocity = movement * moveSpeed;
    }

Why would it accellerate, you’re setting the velocity to a fixed value which is a set speed in a certain direction. Also, gravity works by changing the velocity which again is being stomped over by you setting it explicitly.

If you want acceleration then add forces using AddForce. It might be worth looking at a few tutorials on movement using physics such as here.

As a side note, 2D physics uses 2D vectors so using 3D vectors is kind of redundant.

Anwyay, hope that helps.

Okay it moves properly now using Velocity but it still gets stuck at the corner of platforms so i guess that was not the issue why it got stuck there in the first place?

If you’re still setting velocity directly then physics obviously cannot change the velocity (you stomp over it) so it can fall correctly or correctly move out of overlaps such as you have with your platform.

I am sorry can u tell me what u mean with setting it directly? You mean setting it in the Fixed Update or do you mean the CheckSpeed Method because i deactiavted that for the test.

This is setting it directly:

The physics system modifies the velocity due to gravity, collisions (etc) and you’re stomping over it with your value. This is why you use forces because they add to the existing velocity, not overwrite it.

I would suggest looking at the tutorial link I provided above.