Advertisement
ChrisTutorials

TouchingDirections.cs course reference

Feb 27th, 2023
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(Collider2D), typeof(Animator))]
  6. public class TouchingDirections : MonoBehaviour
  7. {
  8.     public ContactFilter2D touchingFilter;
  9.     public float groundDistance = 0.05f;
  10.     public float wallDistance = 0.2f;
  11.     public float ceilingDistance = 0.05f;
  12.     public Vector2 wallCheckDirection = Vector2.zero;
  13.  
  14.     private Animator animator;
  15.     private CapsuleCollider2D col;
  16.  
  17.     [SerializeField]
  18.     private bool _isOnWall;
  19.  
  20.     public bool IsOnWall
  21.     {
  22.         get
  23.         {
  24.             return _isOnWall;
  25.         }
  26.         set
  27.         {
  28.             _isOnWall = value;
  29.             animator.SetBool(AnimationStrings.isOnWall, value);
  30.         }
  31.     }
  32.  
  33.     [SerializeField]
  34.     private bool _isOnCeiling;
  35.  
  36.     public bool IsOnCeiling
  37.     {
  38.         get
  39.         {
  40.             return _isOnCeiling;
  41.         }
  42.         set
  43.         {
  44.             _isOnCeiling = value;
  45.             animator.SetBool(AnimationStrings.isOnCeiling, value);
  46.         }
  47.     }
  48.  
  49.     [SerializeField]
  50.     private bool _isGrounded;
  51.  
  52.     public bool IsGrounded
  53.     {
  54.         get { return _isGrounded; }
  55.         set
  56.         {
  57.             // If the value is different than what it was before, interrupt ground or air states
  58.             if (_isGrounded = true && value != true)
  59.                 animator.SetTrigger(AnimationStrings.ground_interrupt);
  60.             else if (_isGrounded = false && value != false)
  61.                 animator.SetTrigger(AnimationStrings.air_interrupt);
  62.  
  63.             _isGrounded = value;
  64.             animator.SetBool(AnimationStrings.isGrounded, value);
  65.         }
  66.     }
  67.  
  68.     private RaycastHit2D[] groundHits = new RaycastHit2D[5];
  69.     private RaycastHit2D[] wallHits = new RaycastHit2D[5];
  70.     private RaycastHit2D[] ceilingHits = new RaycastHit2D[5];
  71.  
  72.     private void Awake()
  73.     {
  74.         animator = GetComponent<Animator>();
  75.         col = GetComponent<CapsuleCollider2D>();
  76.  
  77.         // Check that collider offset is 0,0
  78.         if(col.offset.x != 0)
  79.         {
  80.             Debug.LogWarning("Recommended x offset of 0 for TouchingDirections collider on game object " + gameObject.name + ". Adjust the transform instead");
  81.         }
  82.     }
  83.  
  84.     public void FixedUpdate()
  85.     {
  86.         IsGrounded = col.Cast(Vector2.down, touchingFilter, groundHits,  groundDistance) > 0;
  87.         IsOnWall = col.Cast(wallCheckDirection, touchingFilter, wallHits, wallDistance) > 0;
  88.  
  89.         // Wall collisions sometimes show up as ceiling collisions if BoxCollider (I guess corners could show up as either or both)
  90.         IsOnCeiling = col.Cast(Vector2.up, touchingFilter, ceilingHits, ceilingDistance) > 0;
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement