Advertisement
ChrisTutorials

Knight.cs Course Reference Script

Feb 27th, 2023
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.77 KB | None | 0 0
  1. using UnityEngine;
  2. using static Knight;
  3.  
  4. public class Knight : MonoBehaviour
  5. {
  6.     public float walkSpeed = 3f;
  7.  
  8.     // Per Fixed Frame Lerp towards 0. 1 = 100%
  9.     public float walkSpeedLerpStopRate = 0.2f;
  10.  
  11.     public DetectionZone clifEdgeZone;
  12.  
  13.     public enum WalkableDirection { Left, Right };
  14.  
  15.     [SerializeField]
  16.     private WalkableDirection _walkDirection;
  17.  
  18.     public WalkableDirection WalkDirection
  19.     {
  20.         get
  21.         {
  22.             return _walkDirection;
  23.         }
  24.         set
  25.         {
  26.             // Make changes only if new
  27.             _walkDirection = value;
  28.  
  29.             if (value == WalkableDirection.Left)
  30.             {
  31.                 // Facing left so negative scale
  32.                 transform.localScale = new Vector2(-Mathf.Abs(transform.localScale.x), transform.localScale.y);
  33.                 walkDirectionAsVector2 = Vector2.left;
  34.             }
  35.             else if (value == WalkableDirection.Right)
  36.             {
  37.  
  38.                 // Facing right so positive scale
  39.                 transform.localScale = new Vector2(Mathf.Abs(transform.localScale.x), transform.localScale.y);
  40.                 walkDirectionAsVector2 = Vector2.right;
  41.             }
  42.  
  43.             touchingDirections.wallCheckDirection = walkDirectionAsVector2;
  44.         }
  45.     }
  46.  
  47.     private CapsuleCollider2D mainCollider;
  48.     Rigidbody2D rb;
  49.     DetectionZone playerDetector;
  50.     Animator animator;
  51.     SpriteRenderer spriteRenderer;
  52.     TouchingDirections touchingDirections;
  53.     Vector2 walkDirectionAsVector2;
  54.  
  55.     // private Vector2 walkDirection => transform.localScale.x >= 0 ? Vector2.right : Vector2.left;
  56.  
  57.     bool _hasTarget = false;
  58.  
  59.     private bool HasTarget
  60.     {
  61.         get { return _hasTarget; }
  62.         set
  63.         {
  64.             _hasTarget = value;
  65.             animator.SetBool("hasTarget", value);
  66.         }
  67.     }
  68.  
  69.     private bool CanMove
  70.     {
  71.         get
  72.         {
  73.             return animator.GetBool(AnimationStrings.canMove);
  74.         }
  75.     }
  76.  
  77.     public bool LockVelocity { get { return animator.GetBool(AnimationStrings.lockVelocity); } }
  78.  
  79.     public bool IsHit { get { return animator.GetBool(AnimationStrings.isHit); } set { animator.SetBool(AnimationStrings.isHit, value); } }
  80.  
  81.     // Start is called before the first frame update
  82.     void Start()
  83.     {
  84.         rb = GetComponent<Rigidbody2D>();
  85.         playerDetector = GetComponentInChildren<DetectionZone>();
  86.         animator = GetComponent<Animator>();
  87.         mainCollider = GetComponent<CapsuleCollider2D>();
  88.         spriteRenderer = GetComponent<SpriteRenderer>();
  89.         touchingDirections = GetComponent<TouchingDirections>();
  90.         touchingDirections.wallDistance = walkSpeed * 1.5f * Time.fixedDeltaTime;
  91.  
  92.         // Randomize starting walk direction
  93.         WalkDirection = Random.Range(0, 1) == 1 ? WalkableDirection.Left : WalkableDirection.Right;
  94.     }
  95.  
  96.     // Update is called once per frame
  97.     void FixedUpdate()
  98.     {
  99.         touchingDirections.wallCheckDirection = transform.localScale.x > 0 ? Vector2.right : Vector2.left;
  100.  
  101.         // If player walks into zone, set has target to trigger animations
  102.         if (playerDetector.collidersInZone.Count > 0)
  103.         {
  104.             // Targetable enemy is in the zone so try to attack it
  105.             HasTarget = true;
  106.         }
  107.         else
  108.         {
  109.             HasTarget = false;
  110.         }
  111.  
  112.  
  113.         RaycastHit2D[] wallHits = new RaycastHit2D[3];
  114.         // Flip the knights walk direction when it runs into a wall or the edge of a cliff
  115.         if (!IsHit && touchingDirections.IsGrounded && (touchingDirections.IsOnWall || clifEdgeZone.colliderCount == 0))
  116.         {
  117.             FlipWalkDirection();  
  118.         }
  119.        
  120.         // Hit stun overrides movement
  121.         if(IsHit)
  122.         {
  123.             // Hit stun cannot move
  124.             // Velocity set OnHit
  125.         }
  126.         if (CanMove)
  127.         {
  128.             rb.velocity = new Vector2(walkSpeed * walkDirectionAsVector2.x, rb.velocity.y);
  129.         }
  130.         else
  131.         {
  132.             // Default slow towards 0 on x velocity
  133.             rb.velocity = new Vector2(Mathf.Lerp(rb.velocity.x, 0, walkSpeedLerpStopRate), rb.velocity.y);
  134.         }
  135.     }
  136.  
  137.     void FlipWalkDirection()
  138.     {
  139.         if (WalkDirection == WalkableDirection.Left)
  140.         {
  141.             WalkDirection = WalkableDirection.Right;
  142.         }
  143.         else if (WalkDirection == WalkableDirection.Right)
  144.         {
  145.             WalkDirection = WalkableDirection.Left;
  146.         }
  147.         else
  148.         {
  149.             Debug.LogError(name + "'s WalkDirection is not set to Left or Right");
  150.         }
  151.     }
  152.  
  153.     public void OnHit(int damage, Vector2 knockback)
  154.     {
  155.         IsHit = true;
  156.         rb.velocity = new Vector2(knockback.x, knockback.y + rb.velocity.y);
  157.     }
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement