- Home /
2d player can get stuck in walls if you hold down (Not the physics material fixable glitch)
So my movement code is a little unusual for a 2d game but it works the best out of all the code Ive seen online. However, it has one major bug. When you walk against a wall you can stick to it by holding in that direction. (The charcter kinda freaks out and glitched around.) I have tried moving it into fixedupdate but then there is this weird glitch where the movement is really choppy, and it only semi-works. I you know why this is happening I would really appriciate it.
//MOVEMENT [Tooltip("The maximum horizontal movement speed in units per second.")] public float speed = 8; // movenment speed for Luca
[Tooltip("The maximum vertical jump speed in units per second.")]
public float jumpspd = 12; // jumo height for Luca
private float moveInput;
private Rigidbody2D rb;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
//ANIMATION
public Animator anim;
bool canDash = true;
private bool grounded;
bool isRunning;
public SpriteRenderer ren;
//UNUSED AS OF NOW
public static float LucaHP;
public static float LucaSP;
public GameObject hitbox;
public static float damage;
//TODO: MOVE ONTO NEW SCRIPT
// Use this for initialization
void Start()
{
anim = gameObject.GetComponent<Animator>();
anim.SetBool("isRunning", false);
anim.SetBool("dashing", false);
canDash = true;
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
HandleFlip();
HandleJump();
HandleDash();
if (Input.GetButton("Horizontal"))
{
isRunning = true;
}
else
{
isRunning = false;
}
anim.SetBool("isRunning", isRunning);
playermove();
}
void playermove()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
float axixX = Input.GetAxis("Horizontal");
transform.Translate(new Vector2(axixX, 0) * Time.deltaTime * speed);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612154020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
In the breakout tutorial,why does the paddle movement use Update() instead of FixedUpdate()? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Click-to-move questions 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Character getting stuck on walls 2 Answers