EnemyCharge.cs 1.28 KB
Newer Older
Benjamin Lane's avatar
Benjamin Lane committed
1 2 3 4 5 6 7
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyCharge : BaseEnemy
{
	public float DetectionDistance;
8
	public float ChargeSpeed;
Benjamin Lane's avatar
Benjamin Lane committed
9 10
	public bool Charging = false;
	public LayerMask lm;
11
	public bool FallOffLedge = false;
Benjamin Lane's avatar
Benjamin Lane committed
12 13 14

	void FixedUpdate()
	{
15 16 17 18 19 20 21
		Vector2 fwd;
		if (!sr.flipX) {
			// Raycast to check if player is in front with layermask to prevent it from detecting itself
			fwd = transform.TransformDirection (Vector2.right);
		} else {
			fwd = transform.TransformDirection (Vector2.left);
		}
Benjamin Lane's avatar
Benjamin Lane committed
22 23 24 25 26 27 28 29 30 31 32 33 34
		RaycastHit2D hit;
		Debug.DrawRay (transform.position, fwd);
		hit = Physics2D.Raycast (transform.position, fwd, DetectionDistance, lm);

		if (hit)
		{
			if (hit.collider.gameObject.CompareTag ("Player"))
			{//If it sees an object with tag "Player", Charging will be true
				Charging = true;
			}
		}
			
		if (Charging) 
35 36 37 38 39 40 41 42 43 44 45
		{ //Will stop or keep going based on FallOffLedge bool
			if (!FallOffLedge) {
				if (!CheckCollision (fwd, sr.flipX ? leftCollisionPos.position : rightCollisionPos.position)) {
					Charging = false;
					return;
				}
			} else {
				if (!CheckCollisionCharge (fwd, sr.flipX ? leftCollisionPos.position : rightCollisionPos.position)) {
					Charging = false;
					return;
				}
Benjamin Lane's avatar
Benjamin Lane committed
46
			}
47
			Move (fwd);
48
			speed = ChargeSpeed;
Benjamin Lane's avatar
Benjamin Lane committed
49 50 51
		}
	}
}