Search Unity

Feedback Physics2D.OverlapCircleAll showing the box created for debugging purposes

Discussion in 'Scripting' started by Penkine, Jan 19, 2020.

  1. Penkine

    Penkine

    Joined:
    Jun 5, 2019
    Posts:
    15
    I am currently going to use the Physics2D.OverlapCircleAll command to check if my character is on the ground using a LayerMask. and I am wondering how I would show the circle created for debugging, would I need to use a line renderer or there is an attribute I am not using. Any help is much appreciated.

    Current relevant code:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : MonoBehaviour
    4. {
    5.     //  Grounded tools
    6.     [SerializeField] [Tooltip("Position to spawn circle cast.")] private Transform m_GroundCheckPosition;
    7.     [SerializeField] [Tooltip("Layer mask to determine what is ground.")] private LayerMask m_WhatIsGround;
    8.     const float k_GroundedRadius = 0.2f;    // Radius of the overlap circle to determine if grounded.
    9.     private bool m_Grounded;                // Whether or not the player is grounded.
    10.  
    11.     private Rigidbody2D m_Rigidbody2D;
    12.     private Vector3 velocity = Vector3.zero;
    13.  
    14.     private void Awake()
    15.     {
    16.         m_Rigidbody2D = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     private void FixedUpdate()
    20.     {
    21.         m_Grounded = CheckSphere(m_GroundCheckPosition, k_GroundedRadius, m_WhatIsGround);      //  Checking if the player is on ground.
    22.     }
    23.  
    24.     private bool CheckSphere(Transform CheckPosition, float CheckRadius, LayerMask CheckingFor)
    25.     {
    26.         bool result;
    27.         result = false;
    28.  
    29.         Collider2D[] colliders = Physics2D.OverlapCircleAll(CheckPosition.position, CheckRadius, CheckingFor);
    30.         for (int i = 0; i < colliders.Length; i++)
    31.         {
    32.             if (colliders[i].gameObject != gameObject)
    33.                 result = true;
    34.         }
    35.  
    36.         return result;
    37.     }
    38. }
    39.  
    40.