60 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PhysicsCheck : MonoBehaviour
{
private CapsuleCollider2D coll;
[Header("检测参数")]
public bool manual;//手动
public Vector2 bottomOffest;//偏移
public Vector2 leftOffest;
public Vector2 rightOffest;
public float checkRaduis;
public LayerMask groundLayer;
[Header("状态")]
public bool isGround;
public bool touchLeftWall;//接触左墙
public bool touchRightWall;
private void Awake()
{
coll = GetComponent<CapsuleCollider2D>();
if (!manual)//不手动调整的默认边界值
{
rightOffest = new Vector2((coll.bounds.size.x+ coll.offset.x) / 2, coll.bounds.size.y / 2);
leftOffest = new Vector2(-rightOffest.x, rightOffest.y);
}
}
private void Update()
{
Check();
}
public void Check()
{
//地面
isGround = Physics2D.OverlapCircle((Vector2)transform.position+bottomOffest, checkRaduis, groundLayer);
//墙
touchLeftWall = Physics2D.OverlapCircle((Vector2)transform.position + leftOffest, checkRaduis, groundLayer);
touchRightWall = Physics2D.OverlapCircle((Vector2)transform.position + rightOffest, checkRaduis, groundLayer);
}
private void OnDrawGizmosSelected()
{
Gizmos.DrawWireSphere((Vector2)transform.position + bottomOffest, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + leftOffest, checkRaduis);
Gizmos.DrawWireSphere((Vector2)transform.position + rightOffest, checkRaduis);
}
}