using System.Collections; using System.Collections.Generic; using UnityEngine; /* -------------------------------------- Project: Programing assessment Standard: 91906 (AS3.7) v.1 School: Tauranga Boys' College Author: Rauputu Noah Phizacklea Date: August 2024 Unity: 2021.3.18f --------------------------------------- */ public class EnemyPatrol : MonoBehaviour { public float speed; // Speed at which the enemy patrols public float distance = 2f; // Distance to check for ground public Transform groundCheck; // Position to check for ground public LayerMask groundLayer; // Layer mask for the ground private bool movingRight = true; // Direction the enemy is moving void Update() { // Move the enemy horizontally based on speed transform.Translate(Vector2.right * speed * Time.deltaTime); // Cast a ray downward from the groundCheck position to detect ground RaycastHit2D groundRay = Physics2D.Raycast(groundCheck.position, Vector2.down, distance, groundLayer); // If no ground is detected, flip the enemy's direction if (groundRay.collider == null) { Flip(); } } void Flip() { // Flip the enemy's direction based on current movement direction if (movingRight) { transform.eulerAngles = new Vector3(0, -180, 0); // Flip to face left movingRight = false; } else { transform.eulerAngles = new Vector3(0, 0, 0); // Flip to face right movingRight = true; } } }