I am making a 2D game in Unity, I modified the code so that the character does not slide when there are slopes on the stage and the only way I found it was with Raycast, however now it does not detect when the character is on the stage ground so you can perform the jump even in the air indefinitely.
I already tried to use Raycast to modify the jump but the character still manages to jump in the air, even the jump and drop animation doesn’t.
Here is the player controller I’m working on:
public Transform groundCheck;
public Transform rayCastOrigin;
public LayerMask groundLayer;
public float groundCheckRadious;
private Rigidbody2D _rigidbody;
private Animator _animator;
private RaycastHit2D _isGrounded;
//This is the variable you originally used
//private bool _isGrounded;
public class PlayerController : MonoBehaviour
{
void Grounded()
{
//This is the instruction you originally had
//_isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadious, groundLayer);
//This is the new instruction used by Raycast
_isGrounded = Physics2D.Raycast(rayCastOrigin.position, -Vector2.up, 100f, groundLayer);
if (_isGrounded != false)
{
Vector2 temp = groundCheck.position;
temp.y = _isGrounded.point.y;
groundCheck.position = temp;
}
}
void Jump()
{
//Is Jumping?
if (Input.GetKeyDown(KeyCode.Space) && _isGrounded == true && _isRolling == false)
{
Debug.Log("Salto");
_rigidbody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
}