Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AI : MonoBehaviour
- {
- public Vector3 beaconLocation;
- public Vector3 ballLocation;
- [SerializeField] float moveSpeed = 6f;
- [SerializeField] float freezeDistance;
- public bool frozen = false;
- // Update is called once per frame
- void Update()
- {
- if (!frozen)
- {
- MoveAI();
- RotateAI();
- }
- }
- void MoveAI()
- {
- if ((beaconLocation.x > 0.8) && (beaconLocation.x < 4.41) && (beaconLocation.y < 2.8) && (beaconLocation.y > -2.8))
- {
- transform.position = Vector2.MoveTowards(transform.position, beaconLocation, moveSpeed * Time.deltaTime);
- }
- }
- void RotateAI()
- {
- Vector3 relativePos = ballLocation - transform.position;
- if (((relativePos.x > freezeDistance) || (relativePos.x < -freezeDistance)) && ((relativePos.y > freezeDistance) || relativePos.y < -freezeDistance))
- {
- float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
- transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement