Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug RigidBody2D Stuck when running into wall and jumping.

Discussion in 'Scripting' started by crackingtutsyt, Mar 26, 2022.

  1. crackingtutsyt

    crackingtutsyt

    Joined:
    Mar 5, 2022
    Posts:
    1
    So im making a 2D Platformer and I can't jump while pressed against a wall or running against it.

    It's a pretty simple issue i'm pretty sure it's caused by my player controller, which applies horizontal and vertical force at the same time and the horizontal overpowers it, I think. I've tried making vertical force higher to compensate but it has to be insanely high to fix the issue and then the jumping doesn't work because the force is crazy high. I've never posted a question before but I was looking everywhere and could not figure out a fix for this. I'm using the template player controller modified a little. If anyone can help that would be amazing! I've been getting so much progress done on my first game but this really does kill the whole flow of the game when you get stuck on a wall and can't get out of it until you face the other way and jump.


    Video of the glitch (although im sure a lot of you know what im talking about already)

    And this is my player controller code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Platformer.Gameplay;
    5. using static Platformer.Core.Simulation;
    6. using Platformer.Model;
    7. using Platformer.Core;
    8.  
    9. namespace Platformer.Mechanics
    10. {
    11.     /// <summary>
    12.     /// This is the main class used to implement control of the player.
    13.     /// It is a superset of the AnimationController class, but is inlined to allow for any kind of customisation.
    14.     /// </summary>
    15.     public class PlayerController : KinematicObject
    16.     {
    17.         [Header("-=Object References=-")]
    18.         public Rigidbody2D rb;
    19.         public Collider2D collider2d;
    20.         public Health health;
    21.         public bool controlEnabled = true;
    22.  
    23.         [Header("-=Horizontal Movement=-")]
    24.         public float maxSpeed = 7;
    25.         public float Speed;
    26.  
    27.         [Header("-=Vertical Movement=-")]
    28.         public float jumpForce = 1f;
    29.         public float jumpTakeOffSpeed = 7;
    30.         public JumpState jumpState = JumpState.Grounded;
    31.  
    32.         [Header("-=Audio=-")]
    33.         public AudioClip jumpAudio;
    34.         public AudioClip respawnAudio;
    35.         public AudioClip ouchAudio;
    36.         public AudioSource audioSource;
    37.  
    38.         private bool stopJump;
    39.  
    40.         bool jump;
    41.         Vector2 move;
    42.         SpriteRenderer spriteRenderer;
    43.         internal Animator animator;
    44.         readonly PlatformerModel model = Simulation.GetModel<PlatformerModel>();
    45.  
    46.         public Bounds Bounds => collider2d.bounds;
    47.  
    48.         void Awake()
    49.         {
    50.             health = GetComponent<Health>();
    51.             audioSource = GetComponent<AudioSource>();
    52.             collider2d = GetComponent<Collider2D>();
    53.             spriteRenderer = GetComponent<SpriteRenderer>();
    54.             animator = GetComponent<Animator>();
    55.         }
    56.  
    57.         protected override void Update()
    58.         {
    59.             if (controlEnabled)
    60.             {
    61.                 move.x = Input.GetAxis("Horizontal");
    62.                 if (jumpState == JumpState.Grounded && Input.GetButtonDown("Jump"))
    63.                     jumpState = JumpState.PrepareToJump;
    64.                 else if (Input.GetButtonUp("Jump"))
    65.                 {
    66.                     stopJump = true;
    67.                     Schedule<PlayerStopJump>().player = this;
    68.                 }
    69.             }
    70.             else
    71.             {
    72.                 move.x = 0;
    73.             }
    74.             UpdateJumpState();
    75.             base.Update();
    76.         }
    77.  
    78.         void UpdateJumpState()
    79.         {
    80.             jump = false;
    81.             switch (jumpState)
    82.             {
    83.                 case JumpState.PrepareToJump:
    84.                     jumpState = JumpState.Jumping;
    85.                     jump = true;
    86.                     stopJump = false;
    87.                     break;
    88.                 case JumpState.Jumping:
    89.                     if (!IsGrounded)
    90.                     {
    91.                         Schedule<PlayerJumped>().player = this;
    92.                         jumpState = JumpState.InFlight;
    93.                     }
    94.                     break;
    95.                 case JumpState.InFlight:
    96.                     if (IsGrounded)
    97.                     {
    98.                         Schedule<PlayerLanded>().player = this;
    99.                         jumpState = JumpState.Landed;
    100.                     }
    101.                     break;
    102.                 case JumpState.Landed:
    103.                     jumpState = JumpState.Grounded;
    104.                     break;
    105.             }
    106.         }
    107.  
    108.         protected override void ComputeVelocity()
    109.         {
    110.             if (jump && IsGrounded)
    111.             {
    112.                 // velocity.y = jumpTakeOffSpeed * model.jumpModifier;
    113.                 velocity.y = jumpTakeOffSpeed * model.jumpModifier * jumpForce;
    114.                 jump = false;
    115.             }
    116.             else if (stopJump)
    117.             {
    118.                 stopJump = false;
    119.                 if (velocity.y > 0)
    120.                 {
    121.                     velocity.y = velocity.y * model.jumpDeceleration;
    122.                 }
    123.             }
    124.  
    125.             if (move.x > 0.01f)
    126.                 spriteRenderer.flipX = false;
    127.             else if (move.x < -0.01f)
    128.                 spriteRenderer.flipX = true;
    129.  
    130.             animator.SetBool("grounded", IsGrounded);
    131.             animator.SetFloat("velocityX", Mathf.Abs(velocity.x) / maxSpeed);
    132.  
    133.             targetVelocity = move * maxSpeed;
    134.         }
    135.  
    136.         public enum JumpState
    137.         {
    138.             Grounded,
    139.             PrepareToJump,
    140.             Jumping,
    141.             InFlight,
    142.             Landed
    143.         }
    144.     }
    145. }
    I've also already tried changing every material I have to frictionless and all that but that does not have an effect at all.

    Appreciate any help!

    Also, not the biggest deal but I've been having trouble making an infinite background and my current one is just multiple layers lined up with parallax effect on each, they end up lagging whenever I get to a new layer thats lined up. Thank you again.
     
    Last edited: Mar 26, 2022
  2. angelgmoreno

    angelgmoreno

    Joined:
    Dec 13, 2023
    Posts:
    1
    Where you able to solve this? Im having the same issue