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
AddForce
instead of setting it explicitly: docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html – CharlehTime.fixedDeltaTime
toVector2 movement = new Vector2(mx * movementSpeed, rb.velocity.y);
Since right now every tick you're multiplyingmx
bymovementSpeed
, possibly creating large value. I would recommendVector2 movement = new Vector2(mx * movementSpeed * Time.fixedDeltaTime, rb.velocity.y);
– DekuDesuAddForce()
worked, thank you very much for this help! – human