#Gravity doesn't work when move on a wall.

1 messages · Page 1 of 1 (latest)

low cove
#

Hi, so I went to make a 2D platformer and I'm here with an issue. In the video you can see that when I move near a wall and then jump, my player jump but much lower than the usual (I press the jump button as long as I can in both cases), can you help me?
You can see my player with all the pictures and my script below this:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

[RequireComponent(typeof(Rigidbody2D))]
[RequireComponent(typeof(CapsuleCollider2D))]
public class AnimationsAndMovementController : MonoBehaviour
{
    [Header("------------------------------Movement Settings------------------------------")]
    [SerializeField] private float moveSpeed = 5f;

    [Header("------------------------------Jump Settings----------------------------------")]
    [SerializeField] private float _jumpForce;
    [SerializeField] private float _jumpTime;
    [SerializeField] private float _jumpMultiplier;
    //[SerializeField] private bool _canMoveVerticaly = false;

    [Header("------------------------------Gravity Settings-------------------------------")]
    [Tooltip("Transform positionn� aux pieds du personnage (child empty object).")]
    [SerializeField] private float _fallMultiplier;
    [SerializeField] private Transform _groundCheck;
    [SerializeField] private Transform _wallCheck;
    [SerializeField] private Vector2 _groundCheckRadius;
    [SerializeField] private Vector2 _wallCheckRadius;
    [SerializeField] private LayerMask _groundLayer;
    [SerializeField] private PhysicsMaterial2D _zeroFrictionMat;
    [SerializeField] private PhysicsMaterial2D _defaultMat;

    [Header("------------------------------Components-------------------------------------")]
    [SerializeField] private Animator _animator;

    private PlayerInput _playerInput;
    private Rigidbody2D _rb;

    private Vector2 _currentMovementInput;
    private Vector2 _checkGroundOverlapBoxPos;```
The rest of the script is above
#

//------------------------------------------Move Variables-------------------------------------------------
private bool _isMovementPressed = false;
private InputAction _moveAction;

//------------------------------------------Gravity Variables----------------------------------------------
private Collider2D _col;
private bool _isGrounded;
private bool _isWallTouched;
private Vector2 _vecGravity;

//------------------------------------------Jump Variables-------------------------------------------------
private bool _isJumpPressed = false;
private bool _isJumping = false;
private InputAction _jumpAction;
private float _jumpCounter;

//------------------------------------------Animations Variables-------------------------------------------
private int _isWalkingHash;


private void Awake()
{
    _playerInput = GetComponent<PlayerInput>();
    _rb = GetComponent<Rigidbody2D>();
    _col = GetComponent<Collider2D>();

    _isWalkingHash = Animator.StringToHash("IsWalking");

    _moveAction = _playerInput.actions["Move"];
    _jumpAction = _playerInput.actions["Jump"];
    
    _moveAction.started += OnMovementInput;
    _moveAction.performed += OnMovementInput;
    _moveAction.canceled += OnMovementInput;

    _jumpAction.started += OnJump;
    _jumpAction.canceled += OnJump;

    _vecGravity = new Vector2(0, -Physics2D.gravity.y);
}

private void OnEnable()
{
    _moveAction?.Enable();
}

private void OnDisable()
{
    _moveAction?.Disable();
}

private void HandleGravity()
{
    _checkGroundOverlapBoxPos = (_groundCheck != null) ? (Vector2)_groundCheck.position : (Vector2)transform.position + Vector2.down * 0.1f;
    _isGrounded = Physics2D.OverlapBox(_checkGroundOverlapBoxPos, _groundCheckRadius, 0, _groundLayer);
    _col.sharedMaterial = _isGrounded ? _defaultMat : _zeroFrictionMat;

    if (_rb.linearVelocityY < 0)
    {
        _rb.linearVelocity -= _vecGravity * _fallMultiplier * Time.fixedDeltaTime;
    }
}```
#
    {
        _currentMovementInput = context.ReadValue<Vector2>();
        _isMovementPressed = _currentMovementInput.sqrMagnitude > 0.01f;
    }

    private void HandleAnimation()
    {
        bool isWalking = _animator.GetBool(_isWalkingHash);

        if (_isMovementPressed && !isWalking)
        {
            _animator.SetBool(_isWalkingHash, true);
        }
        else if (!_isMovementPressed && isWalking)
        {
            _animator.SetBool(_isWalkingHash, false);
        }

        if (_currentMovementInput.x > 0.01f)
            transform.localScale = new Vector3(1, 1, 1);
        else if (_currentMovementInput.x < -0.01f)
            transform.localScale = new Vector3(-1, 1, 1);
    }

    private void Move()
    {
        float vx = _currentMovementInput.x * moveSpeed;
        _rb.linearVelocity = new Vector2(vx, _rb.linearVelocity.y);
    }

    private void HandleJump()
    {
        if(!_isJumping && _isGrounded && _isJumpPressed)
        {
            _isJumping = true;
            _rb.linearVelocityY = _jumpForce;
            _jumpCounter = 0f;
        }

        if(_rb.linearVelocityY > 0f && _isJumping)
        {
            _jumpCounter += Time.fixedDeltaTime;
            if(_jumpCounter >= _jumpTime)
            {
                _isJumping = false;
            }

            float t = _jumpCounter / _jumpTime;
            float currentJumpM = _jumpMultiplier;

            if(t > 0.5f)
            {
                currentJumpM = _jumpMultiplier * (1 - t);
            }

            _rb.linearVelocity += _vecGravity * currentJumpM * Time.fixedDeltaTime;
        }

        if (!_isJumpPressed)
        {
            _isJumping = false;
            _jumpCounter = 0;

            if(_rb.linearVelocityY > 0f)
            {
                _rb.linearVelocity = new Vector2(_rb.linearVelocityX, _rb.linearVelocityY * 0.6f);
            }
        }
    }```
#
    {
        _isJumpPressed = context.ReadValueAsButton();
    }



    private void FixedUpdate()
    {
        Move();
        HandleAnimation();
        HandleGravity();
        HandleJump();

        Debug.Log(_col.sharedMaterial.ToString());

    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(_checkGroundOverlapBoxPos - new Vector2(0, .95f), _groundCheckRadius);
        Gizmos.color = Color.green;
        Gizmos.DrawWireCube(_checkWallOverlapBoxPos, _wallCheckRadius);
    }
}```
#

Sorry if it's deconstructed my script is long asf lmao

lost rock
#

!code

keen kernelBOT
lost rock
#

please use the "large code blocks" for large code blocks

#

though from what you described, i'd guess this is due to friction rather than anything in code

#

does either your player or the wall have a frictionless material? if not, the friction will be applied on contact

low cove
low cove
lost rock
#

couldn't you just have it never have friction?

low cove
#

I don't know lmao, I thought that it was better to have friction when walking on ground

lost rock
#

it depends on how you're moving (with forces vs setting velocity vs etc)

#

also on how air control would be affected

#

you're setting velocity, so friction doesn't matter too much while on the ground