Игрок скользит с наклоных поверхностей Unity 2D
Я задал Physics Material 2D, где Friction для игрока указан 0 для того, что бы игрок не прилипал ко стенам. Но после этого он начал соскальзовать с обычных наклонных поверхностях.
Как это выглядит: https://drive.google.com/file/d/1UL0P0A24X-BPcC7L9ZPcFKec0D8AsNsM/view?usp=sharing
using Inspector;
using UnityEngine;
namespace FuturePast.CharacterSystem
{
public class MoveState : CharacterState
{
[SerializeField, Range(0, 30)] private float _walkSpeed = 10f;
[SerializeField, Range(0, 30)] private float _runSpeed = 20f;
[SerializeField, Range(0, 20)] private float _acceleration = 10f;
[SerializeField, Range(0, 20)] private float _decceleration = 10f;
[SerializeField] Vector2 _cornerToClimb;
[SerializeField, ReadOnlyInspector] public float direction;
[SerializeField, ReadOnlyInspector] public TypeMove typeMove = TypeMove.Walk;
[SerializeField, ReadOnlyInspector] private float _moveSpeed;
private Vector2 _normal;
private void FixedUpdate()
{
Move();
}
private void Move()
{
_moveSpeed = GetSpeed();
float targetSpeed = direction * _moveSpeed;
float speedDif = targetSpeed - rbCache.velocity.x;
float accelRate = Mathf.Abs(targetSpeed) > 0.01f ? _acceleration : _decceleration;
float movement = Mathf.Pow(Mathf.Abs(speedDif) * accelRate, 0.9f) * Mathf.Sign(speedDif);
rbCache.AddForce(movement * Vector2.right);
}
private Vector2 Project(Vector2 forward)
{
return forward - Vector2.Dot(forward, _normal) * _normal;
}
public void NormalDefinition(Collision2D collision, Vector2 cornerToClimb)
{
foreach (var contact in collision.contacts)
{
var normal = contact.normal;
if (IsGround(normal, cornerToClimb))
{
_normal = normal;
break;
}
}
}
private bool IsGround(Vector2 normal, Vector2 cornerToClimb)
{
if (normal.x > cornerToClimb.x) return false;
if (normal.y < cornerToClimb.y) return false;
return true;
}
private float GetSpeed()
{
switch (typeMove)
{
case TypeMove.Walk:
return _walkSpeed;
case TypeMove.Run:
return _runSpeed;
}
return 0;
}
private void OnCollisionEnter2D(Collision2D collision)
{
NormalDefinition(collision, _cornerToClimb);
}
public override void Exit()
{
base.Exit();
direction = 0;
_moveSpeed = 0;
}
}
}