M
M
MegaTochka2022-02-05 21:38:45
C++ / C#
MegaTochka, 2022-02-05 21:38:45

An error in the code in physics 2d and overlapcircle, what should I do?

61fec41016779964003484.png

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

public class PlayerContorller : MonoBehaviour
{
    public float speed;
  public float JumpForce;
  private float moveInput;
  
  private Rigidbody2D rb;
  
  private bool facingRight = true;
  
  private bool isGrounded;
  public Transform feetPos;
  public float checkRadius;
  public LayerMask whatIsGround;
  
  private void Start()
  {
    rb = GetComponent<Rigidbody2D>();
  }
  
  private void FixedUpdate()
  {
    moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    if(facingRight == false && moveInput > 0)
    {
      Flip();
    }
    else if(facingRight == true && moveInput < 0)
    {
      Flip();
    }
  }
  
  private void Update()
  {
    isGrounded = Physics2D.OverLapCircle(feetPos.position, checkRadius, whatIsGround);
    
    if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
    {
      rb.velocity = Vector2.up * jumpForce;
    }
  }
  
  void Flip()
  {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
  }
}


I don't understand where is the error, please help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question