Advertisement
johnnygoodguy2000

EnemyPatrol.cs

Apr 27th, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyPatrol : MonoBehaviour
  6. {
  7.  
  8.     public float moveSpeed;
  9.     public bool movingRight;
  10.  
  11.      public Transform wallCheck;
  12.      public LayerMask whatIsWall;
  13.     public float wallCheckRadius;
  14.  
  15.     private bool notAtEdge;
  16.  
  17.     public Transform edgeCheck;
  18.  
  19.  
  20.  
  21.  
  22.  
  23.    
  24.  
  25.     private bool hittingWall;
  26.     // Start is called before the first frame update
  27.     void Start()
  28.     {
  29.        
  30.     }
  31.  
  32.     // Update is called once per frame
  33.     void Update()
  34.     {
  35.  
  36.         hittingWall = Physics2D.OverlapCircle(wallCheck.position, wallCheckRadius, whatIsWall);
  37.  
  38.         notAtEdge = Physics2D.OverlapCircle(edgeCheck.position, wallCheckRadius, whatIsWall);
  39.  
  40.         if (hittingWall || !notAtEdge)
  41.         {
  42.             movingRight = !movingRight;
  43.         }
  44.  
  45.         if (movingRight)
  46.         {
  47.             transform.localScale = new Vector3(-1f, 1f, 1f);
  48.             GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
  49.         }
  50.         else
  51.         {
  52.             transform.localScale = new Vector3(1f, 1f, 1f);
  53.             GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
  54.         }
  55.        
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement