M
M
MegaTochka2021-08-24 11:04:26
2D
MegaTochka, 2021-08-24 11:04:26

Unity 2d, when turning, the character falls through the ground and does not jump, what should I do?

When turning, the script falls through the collider and is inside the ground, the player cannot jump, what to do about it?

Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



namespace Assets.Scripts
{
    public class PlayerController : MonoBehaviour
    {
        private float moveInput;
        private Rigidbody2D rigidbody;
    private bool facingRight = true;
    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGrounded;
    
        [SerializeField] private float speed;
        [SerializeField] private float jumpForce;
    

        private void Start()
        {
            rigidbody = GetComponent<Rigidbody2D>();
        }

        private void FixedUpdate()
        {
            moveInput = Input.GetAxis("Horizontal");
            rigidbody.velocity = new Vector2(moveInput * speed, rigidbody.velocity.y);
      if(facingRight == false && moveInput > 0)
      {
        Flip();
      }
      else if(facingRight == true && moveInput < 0)
      {
        Flip();
      }
    }
    void update()
    {
      isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGrounded);
      
      if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
      {
        rigidbody.velocity = Vector2.up * jumpForce;
      }
    }
    void Flip()
    {
      facingRight = !facingRight;
      Vector3 Scaler = transform.localScale;
      Scaler.x *= -1;
      transform.localScale = Scaler;
    }
  }
}

Screenshots
6124a64017f1a765522901.png
6124a7340ad9d996955265.png6124a74c2e7c7080059445.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Mailler, 2021-08-25
@MegaTochka

Perhaps you made a mistake not in the code, but in the editor itself.
Well, or when you turn the script changes the position of the player under the Collider
, not enough information as for me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question