Advertisement
johnnygoodguy2000

BossPatrol.cs

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