0
votes

So I am very very very new to unity and I was following this tutorial here on how to make a basic platformer so I could learn basic RigidBody controlling and stuff like that

All of it works fine except for this one thing, whenever I move into a wall I get stuck there until I let go of A or D which are my movement keys.

Here is my code, i've already tried to implement a fix but that didn't work and broke it even further. It will be commented out and highlighted with a comment saying where it is in my code

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

public class movementScript : MonoBehaviour
{
    public float movementSpeed = 50;
    public Rigidbody2D rb;

    public LayerMask groundLayer;

    public float jumpForce = 20f;
    public Transform feet;

    float mx;

    private void Update()
    {
        Debug.Log(transform.position);
        // me trying to fix the error
        // if(isGrounded() && !rb.IsTouchingLayers()){
        //     return;
        // }
        // end of me trying to fix the error

        mx = Input.GetAxis("Horizontal");

        if (Input.GetButtonDown("Jump") && isGrounded())
        {
            jump();
        }
    }

    private void FixedUpdate()
    {
        Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
        rb.velocity = movement;
    }

    void jump()
    {
        Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
        rb.velocity = movement;
    }

    public bool isGrounded()
    {
        Collider2D groundCheck = 
            Physics2D.OverlapCircle(feet.position, 0.5f, groundLayer);
        
        if (groundCheck)
        {
            return true;
        }
        return false;
    }
}

Here is what my project looks like if you need it. image of project here

1
You are overriding the forces set by the game engine with your own movement vector. What you probably want is AddForce instead of setting it explicitly: docs.unity3d.com/ScriptReference/Rigidbody.AddForce.htmlCharleh
Actually, ignore that, I see you are already taking the existing Y velocity - it does sound like your grounding check is assuming you are grounded. Have you debugged the ground check? The circle you are using may be wider than your character and therefore detecting the ground when you are touching a wall.Charleh
@Charleh This should not be an issue because my boundaries are not in the ground layerhuman
Try adding Time.fixedDeltaTime to Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y); Since right now every tick you're multiplying mx by movementSpeed, possibly creating large value. I would recommend Vector2 movement = new Vector2(mx * movementSpeed * Time.fixedDeltaTime, rb.velocity.y);DekuDesu
@Charleh the original answer with AddForce() worked, thank you very much for this help!human

1 Answers

0
votes

Ok guys so by using Charleh's answer I have came to a solution that works almost perfectly, only thing I dont like is the fact that its very slow to control. What I mean by that is that its acceleration and stopping speeds are very low. But it does work and controls like it did before and now it doesn't get stuck to walls.

So what I did was just change my original code to this

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

public class movementScript : MonoBehaviour {
    public float movementSpeed = 20;
    public Rigidbody2D rb;

    public LayerMask groundLayer;

    public float jumpForce = 20f;
    public Transform feet;

    float mx;

    private void Update() {
        Debug.Log(transform.position);
        mx = Input.GetAxis("Horizontal");
        if (Input.GetButtonDown("Jump") && isGrounded())
        {
            jump();
        }
    }

    private void FixedUpdate() {
        Vector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
        rb.AddForce(movement);
    }

    void jump() {
        Vector2 movement = new Vector2(rb.velocity.x, jumpForce);
        rb.velocity = movement;
    }

    public bool isGrounded() {
        Collider2D groundCheck =  Physics2D.OverlapCircle(feet.position, 0.5f, groundLayer);
        if (groundCheck) {
            return true;
        }
        return false;
    }
}

And I changed the linear drag of my player's RigidBody2D to 1