Advertisement
Schicklerdev

AI.cs

Jan 16th, 2022
38
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AI : MonoBehaviour
  6. {
  7.     public Vector3 beaconLocation;
  8.     public Vector3 ballLocation;
  9.     [SerializeField] float moveSpeed = 6f;
  10.     [SerializeField] float freezeDistance;
  11.     public bool frozen = false;
  12.  
  13.     // Update is called once per frame
  14.     void Update()
  15.     {
  16.         if (!frozen)
  17.         {
  18.             MoveAI();
  19.             RotateAI();
  20.         }
  21.     }
  22.  
  23.     void MoveAI()
  24.     {
  25.         if ((beaconLocation.x > 0.8) && (beaconLocation.x < 4.41) && (beaconLocation.y < 2.8) && (beaconLocation.y > -2.8))
  26.         {
  27.             transform.position = Vector2.MoveTowards(transform.position, beaconLocation, moveSpeed * Time.deltaTime);
  28.         }
  29.     }
  30.  
  31.     void RotateAI()
  32.     {
  33.         Vector3 relativePos = ballLocation - transform.position;
  34.         if (((relativePos.x > freezeDistance) || (relativePos.x < -freezeDistance)) && ((relativePos.y > freezeDistance) || relativePos.y < -freezeDistance))
  35.         {
  36.             float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
  37.             transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
  38.         }
  39.     }
  40.  
  41.  
  42. }
  43.  
Advertisement
RAW Paste Data Copied
Advertisement