Advertisement
GUPPYYYY

Ai Chase

Oct 31st, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AiChase : MonoBehaviour
  6. {
  7.     public Transform playerTransform;
  8.     public bool isChasing;
  9.     public float chaseDistance;
  10.     public float moveSpeed;
  11.     void Start()
  12.  
  13.     // Update is called once per frame
  14.     void Update()
  15.     {
  16.  
  17.         if (isChasing)
  18.         {
  19.             if (transform.position.x > playerTransform.position.x)
  20.             {
  21.                 transform.position += Vector3.left * moveSpeed * Time.deltaTime;
  22.             }
  23.             if (transform.position.x < playerTransform.position.x)
  24.             {
  25.                 transform.position += Vector3.right * moveSpeed * Time.deltaTime;
  26.             }
  27.         }
  28.         else
  29.         {
  30.             if (Vector2.Distance(transform.position, playerTransform.position) < chaseDistance)
  31.             {
  32.                 isChasing = true;
  33.             }
  34.  
  35.         }
  36.  
  37.  
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement