- Home /
The question is answered, right answer was accepted
2D character gets stuck when falling
Hello.
I ran into a problem i have no clue how to fix. Whenever my 2D character is falling and i get in touch with a wall, the character tends to get stuck mid-air when pressing in the same direction as the wall. What i want, is to fall down to the ground, whether i'm touching my keyboard or not. How does one fix this problem? I'm still pretty new to coding so please explain to me like i'm 5.
Thank you for helping.
The code (C#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Character : MonoBehaviour
{
Rigidbody2D rb;
private Animator anim;
public float speed;
private float moveInput;
private bool isGrounded;
public Transform feetpos;
public float checkradius;
public LayerMask WhatisGround;
public float Jumpforce;
AudioSource audio;
public AudioClip ActualJump2D;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, -180, 0);
}
if (moveInput == 0)
{
anim.SetBool("isRunning", false);
anim.SetBool("isIdle", true);
}
else
{
anim.SetBool("isRunning", true);
anim.SetBool("isIdle", false);
}
isGrounded = Physics2D.OverlapCircle(feetpos.position, checkradius, WhatisGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("isJumping", true);
rb.velocity = Vector2.up * Jumpforce;
audio.PlayOneShot(ActualJump2D, 0.3F);
}
if (rb.velocity.y == 0)
{
anim.SetBool("isJumping", false);
}
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name.Equals("Grass Tile [moving]"))
this.transform.parent = col.transform;
else if (col.gameObject.name.Equals("Grass Tile20 [moving]"))
this.transform.parent = col.transform;
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.name.Equals("Grass Tile [moving]"))
this.transform.parent = null;
else if (col.gameObject.name.Equals("Grass Tile20 [moving]"))
this.transform.parent = null;
}
}
Answer by DigitalWizrd · Nov 10, 2020 at 04:41 PM
It's possible that your collider component is colliding with a wall or possibly "overlapping" into the wall causing your velocity to come to a halt. This could be caused by updating your horizontal position without checking to see if you're hitting a wall.
For example, you provide a positive horizontal input, so the player will move to the right according to your 'speed' value. This movement isn't a smooth transition from point A to point B. For lack of a better term the physics update 'teleports' you to the right a certain distance according to your speed value.
Try adjusting the speed value from a low value to a really high value to see what I mean. Unity can't predict that the speed you are travelling will move you into a collider, unless you tell it to check for a wall before performing the move operation.
So my suggestion is before your fixedupate gives a new velocity, check to confirm that there is nothing your character would hit horizontally. And if there is, shorten the distance by giving a smaller speed value for just a moment.
Hope that helps, I haven't had to solve this in a long time so I could be off.
Hello. Sorry for the late reply, i was busy with some irl stuff. The solution you gave actually worked! However, i found a different solution that also works fine. I simply added a 'Physics $$anonymous$$aterial 2D' material that functions as a slippery material where you simply slide downwards at normal speed when colliding with the wall. Anyway, thanks for helping a fellow coder in need!