Weird Jump After Walking Into Wall and Releasing
Thanks for reading this. I've been working on a prototype for a metroidvania, and I got the basic walk, run, and jump down, but for some reason, after walking against a wall and releasing the walk button, the player springs into the air at a height around 1.5x the max jump height is set. I've tried to lock the y position when not jumping and not moving, but because of how I detect input, I can't find a way to make that work, so I tried to assign a physics material with a very small amount of friction, which does remove the jump bug, but then prevents my character from jumping when in a corner sometimes. At some point, I may use a physics material for a wall jump drag, so if possible, I would like to keep the friction 0 for as long as possible until I do this. Thanks for helping me out.
using System.Collections; using System.Collections.Generic; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine; public class PlayerMovement : MonoBehaviour { Material material; float lastSpawnSet; public LayerMask dangerLayer; public bool inDanger; public float dangerCheckRadius; public float xMovement; float yMovement; public float gravityScale; public float direction; public Transform playerTransform; Rigidbody2D rb; PlayerInput playerControls; InputAction movement; InputAction jump; InputAction run; public float speed; public float walkingSpeed; public float runningSpeed; public float jumpSpeed; public bool grounded; public LayerMask groundLayer; public Transform groundCheck; public float groundCheckRadius; public bool frozen; public float coolDown; public float nextMoveTime; public Vector2 spawn; public Vector2 playerFallBack; public bool runHeld; void Awake() { GameManager.playerManager = gameObject.GetComponent<PlayerManager>(); DontDestroyOnLoad(gameObject); frozen = false; playerControls = new PlayerInput(); movement = playerControls.Player.Move; jump = playerControls.Player.Jump; run = playerControls.Player.Run; material = gameObject.GetComponent<Material>(); } void Start() { rb = this.GetComponent<Rigidbody2D>(); playerTransform = gameObject.transform; speed = walkingSpeed; } void OnEnable() { playerControls.Enable(); } void OnCollisionEnter2D (Collision2D targetObj) { if (targetObj.gameObject.tag == "Void" || targetObj.gameObject.tag == "Trap") { GameManager.playerManager.Death(); } } void FixedUpdate() { if (frozen) rb.gravityScale = 0; else rb.gravityScale = gravityScale; if (Time.time > nextMoveTime) frozen = false; grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, groundLayer); if (!frozen) xMovement = movement.ReadValue<Vector2>().x; if (!frozen) { rb.velocity = new Vector2(xMovement * speed, rb.velocity.y); } if (xMovement > 0) { rb.constraints |= RigidbodyConstraints2D.FreezeRotation; direction = 1; } if (xMovement < 0) { rb.constraints |= RigidbodyConstraints2D.FreezeRotation; direction = -1; } jump.performed += ctx => { if (ctx.ReadValue<float>() > 0.5f) { if (grounded && !frozen) { rb.velocity = new Vector2(rb.velocity.x, jumpSpeed); } } }; run.performed += ctx => { if (ctx.ReadValue<float>() > 0.5f) { if (grounded && !frozen) { runHeld = true; } } }; run.canceled += ctx => { if (ctx.ReadValue<float>() <= 0.5f) { runHeld = false; } }; if (runHeld && xMovement != 0) { float t = 0; t += (Time.deltaTime/1); speed = Mathf.Lerp(speed, runningSpeed, t); } else if (!runHeld && xMovement != 0) { float t = 0; t += (Time.deltaTime/1); speed = Mathf.Lerp(speed, walkingSpeed, t); } else { speed = walkingSpeed; } if (direction == 1) transform.localScale = new Vector2 (1, 1); if (direction == -1) transform.localScale = new Vector2 (-1, 1); if (grounded && xMovement == 0) { rb.constraints |= RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation; } else { rb.constraints &= ~RigidbodyConstraints2D.FreezePositionX; } grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer); inDanger = Physics2D.OverlapCircle(groundCheck.position, dangerCheckRadius, dangerLayer); if (grounded) { if (!inDanger) { if (Time.realtimeSinceStartup >= lastSpawnSet + 2) { if (Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, dangerLayer) == false) { lastSpawnSet = Time.realtimeSinceStartup; playerFallBack = new Vector2(playerTransform.position.x, playerTransform.position.y); } } } } } }
Edit: Quick update. I decided to use a physics material 2d, as well as a platform effector to prevent the player from sticking to walls. The original problem still persists.
Sorry my friend but that code is really tough to read. FYI to post code on the forum here a line needs to be indented four spaces. For example
public float myfloat;
Now put 4 spaces before the word public
public float myfloat;
So go through your post by editing the lines with the correct indentation so we can read what you got there.
Your answer
![](https://koobas.hobune.stream/wayback/20220613032010im_/https://answers.unity.com/themes/thub/images/avi.jpg)