Skip to content

Instantly share code, notes, and snippets.

@HassakuTb
Created July 18, 2016 23:34
Show Gist options
  • Save HassakuTb/9b8aa4a375e9102fb1a32243cbd4658b to your computer and use it in GitHub Desktop.
Save HassakuTb/9b8aa4a375e9102fb1a32243cbd4658b to your computer and use it in GitHub Desktop.
Boxcast to axis aligned direction.
/**
NYSL Version 0.9982 (en) (Unofficial)
----------------------------------------
A. This software is "Everyone'sWare". It means:
Anybody who has this software can use it as if he/she is
the author.
A-1. Freeware. No fee is required.
A-2. You can freely redistribute this software.
A-3. You can freely modify this software. And the source
may be used in any software with no limitation.
A-4. When you release a modified version to public, you
must publish it with your name.
B. The author is not responsible for any kind of damages or loss
while using or misusing this software, which is distributed
"AS IS". No warranty of any kind is expressed or implied.
You use AT YOUR OWN RISK.
C. Copyrighted to Hassaku
D. Above three clauses are applied both to source and binary
form of this software.
*/
using UnityEngine;
/// <summary>
/// 上下左右の4方向にBoxcastして接触判定をするためのクラス
///
/// Boxcast to AxisAlignedDirection.
/// To judge actor touch landshape or do not.
/// </summary>
public class DirectionalBoxcaster : MonoBehaviour{
#region enum direction
/// <summary>
/// direction axis sligned.
/// </summary>
public enum AxisAlignedDirection {
Up,
Down,
Right,
Left,
}
public static class AxisAlignedDirectionExtension {
public static Vector2 ToVector(this AxisAlignedDirection dir) {
switch (dir) {
case AxisAlignedDirection.Up:
return Vector2.up;
case AxisAlignedDirection.Down:
return Vector2.down;
case AxisAlignedDirection.Right:
return Vector2.right;
case AxisAlignedDirection.Left:
return Vector2.left;
default:
throw new System.NotImplementedException("unreachable code");
}
}
}
#endregion
/// <summary>
/// 判定に利用するBoxCollider
/// collider
/// </summary>
public BoxCollider2D Collider;
/// <summary>
/// direction for cast
/// </summary>
public AxisAlignedDirection Direction;
/// <summary>
/// ColliderとLandshapeの距離がこの値より小さければ接触と判定する
/// object is touching if distance smaller than this value.
/// </summary>
public float Fazziness = 0.01f;
/// <summary>
/// 地形のレイヤーマスク
/// layer mask of ground collider
/// </summary>
public LayerMask LandshapeLayerMask;
/// <summary>
/// 接触しているかどうかを取得する
/// is touching?
/// </summary>
public bool IsTouching { get; private set; }
/// <summary>
/// 接触しているときのRaycastのコリジョン状態を取得します
/// get collided ground object info
/// </summary>
public RaycastHit2D RaycastHit { get; private set; }
protected void FixedUpdate() {
// BoxColliderの真下に短くぶっといRaycast(BoxCast)を撃つ
// 衝突すれば接地中
// すでに地面にめり込んでいる場合は判定されないので少し上から撃つ
// FloatingDirectionより地面との距離が小さいならば接地と判定
RaycastHit = Physics2D.BoxCast(
origin: new Vector2(Collider.transform.position.x, Collider.transform.position.y) + Collider.offset,
size: Collider.size,
angle: 0f,
direction: Direction.ToVector(),
distance: Fazziness,
layerMask: LandshapeLayerMask
);
IsTouching = RaycastHit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment