Advertisement
GUPPYYYY

EnemyPatrol

Oct 31st, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class enemyPatrol : MonoBehaviour
  6. {
  7.     public GameObject pointA;
  8.     public GameObject pointB;
  9.     private Rigidbody rb;
  10.     private Transform currentPoint;
  11.     public float speed;
  12.    
  13.  
  14.     void Start()
  15.     {
  16.         rb = GetComponent<Rigidbody>();
  17.         currentPoint = pointB.transform;
  18.     }
  19.  
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.  
  24.  
  25.             Vector2 point = currentPoint.position - transform.position;
  26.             if (currentPoint == pointB.transform)
  27.             {
  28.                 rb.velocity = new Vector2(speed, 0);
  29.             }
  30.             else
  31.             {
  32.                 rb.velocity = new Vector2(-speed, 0);
  33.             }
  34.             if (Vector2.Distance(transform.position, currentPoint.position) < 2f && currentPoint == pointB.transform)
  35.             {
  36.                 currentPoint = pointA.transform;
  37.             }
  38.             if (Vector2.Distance(transform.position, currentPoint.position) < 2f && currentPoint == pointA.transform)
  39.             {
  40.                 currentPoint = pointB.transform;
  41.             }
  42.        
  43.            
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement