I_wanna_be_a_Fox/Assets/Script/RaycastController.cs
Koha9 a80f3d6560 I wanna be a fox first commit.
Initial commit. Complete project for 'I Wanna Be A Fox' - a 2D scrolling game inspired by 'I Wanna Be The Guy'.
2024-02-22 07:27:59 +09:00

61 lines
1.7 KiB
C#

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(BoxCollider2D))]
public class RaycastController : MonoBehaviour
{
public LayerMask collisionMask;
public const float skinWidth = .015f;
public int horizontalRayCount = 4;
public int verticalRayCount = 4;
[HideInInspector]
public float horizontalRaySpacing;
[HideInInspector]
public float verticalRaySpacing;
[HideInInspector]
public BoxCollider2D thisCollider;
public RaycastOrigins raycastOrigins;
public virtual void Awake()
{
thisCollider = GetComponent<BoxCollider2D>();
}
public virtual void Start()
{
CalculateRaySpacing();
}
public void UpdateRaycastOrigins()
{
Bounds bounds = thisCollider.bounds;
bounds.Expand(skinWidth * -2);
raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y);
raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y);
raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y);
raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y);
}
public void CalculateRaySpacing()
{
Bounds bounds = thisCollider.bounds;
bounds.Expand(skinWidth * -2);
horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue);
verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue);
horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1);
verticalRaySpacing = bounds.size.x / (verticalRayCount - 1);
}
public struct RaycastOrigins
{
public Vector2 topLeft, topRight;
public Vector2 bottomLeft, bottomRight;
}
}